1

I'm trying to replace a text using replaceAll method but getting this error

Caused by: java.util.regex.PatternSyntaxException: Illegal repetition near index 0
${rate+0.0020D2}

here is my code

String regExp = "\\$\\{rate[+-]\\d+(\\.\\d+)D[0-9]\\}";
String text = "${rate+0.0020D2},banana,${rate-0.4002D3},${rate+0.2003D4},${rate+bananD4},${rate+.123.415D4}";

Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(text);

String match = null;

List<String> matches = new ArrayList<String>(5);
while (matcher.find()) {

    int startIndex = matcher.start();
    int endIndex = matcher.end();
    match = matcher.group();
    matches.add(match.substring(2, match.length() - 1));

}
String[] results = matches.toArray(new String[0]);
for(int i=0;i <= results.length;i++){
text.replaceAll(results[i],"<span class=\"rate\""+i+">"+results[i]+ "</span>");
}

i can resolve this if i use text.replaceAll("\\$\\{rate+0.0020D2\\}","<span class=\"rate\""+i+">"+results[i]+ "</span>");

but i can't do this because my value is in variable . is there any solution for this

user2142786
  • 1,484
  • 9
  • 41
  • 74
  • 2
    The error message tells you the problem: The curly brackets are used for repetition indication. The regular expression "${rate ..." is thus not valid. – Seelenvirtuose Mar 27 '15 at 07:23
  • Use `replace` instead of `replaceAll` if you don't want the string to be replaced be a regex – halex Mar 27 '15 at 07:24

1 Answers1

1

Your regular expression is ${rate+0.0020D2}. You need to escape it:

text.replaceAll(Pattern.quote(results[i]),"<span class=\"rate\""+i+">"+results[i]+ "</span>");
agad
  • 2,192
  • 1
  • 20
  • 32
  • doesn't work still getting this exception java.lang.IllegalArgumentException: Illegal group reference – user2142786 Mar 27 '15 at 07:56
  • @user2142786 Use `Matcher.quoteReplacement(" – halex Mar 27 '15 at 08:05
  • but it's not creatin span structure its simply printing the value – user2142786 Mar 27 '15 at 08:15
  • @user2142786 Does the line `text.replaceAll(Pattern.quote(results[i]), Matcher.quoteReplacement(""+results[i]+ ""));` work? – halex Mar 27 '15 at 08:20
  • yes it works but its simply printing the value not whole structure like – user2142786 Mar 27 '15 at 08:49
  • What is the problem exactly now? – agad Mar 27 '15 at 08:59
  • i think replaceAll method is not working because it is searching for \Q${rate+0.0020D2}\E instead of ${rate+0.0020D2} due to which he is not replacing it from html page – user2142786 Mar 27 '15 at 09:01
  • According to documentation it should work. You can try *replace* instead of *replaceAll* as you you have the concrete string, in the moment of replacing, not a regex. – agad Mar 27 '15 at 09:13
  • i got it the reason was The thing that jumps out is that replaceAll returns a string with the changes made in it, it doesn't change the string you call it on (it can't, strings are immutable). So: text = text.replaceAll(Pattern.quote(matches.get(i)),Matcher.quoteReplacement(""+matches.get(i)+ "")); – user2142786 Mar 27 '15 at 09:26