Reputation: 1482
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
Upvotes: 1
Views: 1937
Reputation: 2189
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>");
Upvotes: 1