0

is there any efficient way to do multiple replaceAll("match","replace").

I'm finding match using regex and replace. e.g.

public static String replacePattern(String line)
{
 if(line != null || !"".equals(line)) 
 {
 line = line.replaceAll("regexPattern_1","replacce_1");
 line = line.replaceAll("regexPattern_2","replacce_2"); 
 line = line.replaceAll("regexPattern_3","replacce_3");
 line = line.replaceAll("regexPattern_4","replacce_4");
 .
 .
 .
 line = line.replaceAll("regexPattern_N","replacce_N");
 return line;
 }
 return line;
}

I don't want code look good, to me Perf is important

asked Apr 16, 2016 at 10:56
11
  • There are shorter ways of writing the code sure, but not really any more efficient way of doing, at least not noteworthy so. Think, like a computer, of how many comparisons are needed to do it. Commented Apr 16, 2016 at 10:59
  • For ALL cases? Probably not. Depends on your specific requirements. Commented Apr 16, 2016 at 11:01
  • Just place the pattern-replace pairs in a HashMap and write a loop Commented Apr 16, 2016 at 11:01
  • @leonbloy That's not really more efficient, which is what he asked about. But I agree it looks cleaner Commented Apr 16, 2016 at 11:04
  • Is scenario like replaceAll("a","b") and then replaceAll("b","c") possible? What result would you want to achieve for input like abc? Should it be bcc or ccc? Commented Apr 16, 2016 at 11:05

3 Answers 3

1

Since Strings are immutable another way to handle this:

str.replaceAll("a", "b").replaceAll("c", "d").replaceAll("e", "f");
answered Apr 16, 2016 at 11:05
Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure what you mean by shorter but if you want shorter texts to write you can use:

line = line.replaceAll("regexPattern_1","replacce_1")
 .replaceAll("regexPattern_2","replacce_2") 
 .replaceAll("regexPattern_3","replacce_3")
 .replaceAll("regexPattern_4","replacce_4")
 .
 .
 .
 .replaceAll("regexPattern_N","replacce_N");

Or:

String[] patterns = new String[] {"pattern1",....};
String[] replace = new String[] {"replace1",....};
for(int i=0; i<patterns.length; i++)
{
 line = line.replaceAll(patterns[i],replace[i]);
}
answered Apr 16, 2016 at 11:02

Comments

1

Not sure whether efficiency could be improved. But you can make it look good. If you want more elegance, you can use recursion which will need some additional work for termination.

public static String replacePattern(String line)
{
 //intitialize String[] repl with items to be replaces
 //initialize String[] pattern with corresponding replacements
 for(int i = 0; i<repl.length; i++)
 if(line != null || !"".equals(line)) 
 line = line.replaceAll(repl[i],pattern[i]);
 return line;
}
answered Apr 16, 2016 at 11:12

4 Comments

what do you mean by recursion?
Recursively passing line, arrays replacement strings, array of pattern strings, position parameter. Use position parameter to build a base case
not a good solution for this case which can be solved with one line for loop
Yeah. I just mentioned that it is possible and will look elegant.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.