How to replace multiple reconciled regex

I have a set of regular expressions that need to be applied to a String set,

For example:

  • all multiple spaces with one space ("\s{2,}" --> " ")
  • everything. and then char c. followed by a space followed by char(\.([a-zA-Z]-->". $1")

So, I will have something like this:

String s="hello     .how are you?";
s=s.replaceAll("\\s{2,}"," ");
s=s.replaceAll("\\.([a-zA-Z])",". $1");
....

it works, however, imagine that I am trying to replace more than 100 such expressions with a long string. it is useless to say how slowly it can be.

so my question is is there a more efficient way to generalize these replacements with single replaceAll (or something similar like Pattern / Matcher for example)

I followed Java Replacing several different ... ,

but the problem is that my regular expressions are not simple Strings.

+4
2

2 replaceAll :

s = s.replaceAll("\\s{2,}"," ");
s = s.replaceAll("\\.([a-zA-Z])",". $1");

replaceAll :

s = s.replaceAll("\\s{2,}|(\\.)(?=[a-zA-Z])", "$1 ");

- RegEx

+3

.

Map<Integer, Function<Matcher, String>>.

  • Lambdas

, , . .

Map<Integer, Function<Matcher, String>> replacements = new HashMap<>() {{
    put(1, matcher -> "");
    put(2, matcher -> " " + matcher.group(2));
}};

String input = "lorem substr1 ipsum substr2 dolor substr3 amet";

// create the pattern joining the keys with '|'. Need to add groups for referencing later
String regexp = "(\\s{2,})|(\\.(?:[a-zA-Z]))";

StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile(regexp);
Matcher m = p.matcher(input);

while (m.find()) {
    //TODO change to find which groupNum matched
    m.appendReplacement(sb, replacements.get(m.group(groupNum)));
}
m.appendTail(sb);


System.out.println(sb.toString());   // lorem repl1 ipsum repl2 dolor repl3 amet
+1

Source: https://habr.com/ru/post/1619213/


All Articles