I'm trying to replace first occurence of String
matching my regex, while iterating those occurences like this:
(this code is very simplified, so don't try to find some bigger sense of it)
Matcher tagsMatcher = Pattern.compile("\\{[sdf]\\}").matcher(value);
int i = 0;
while (tagsMatcher.find()) {
value = value.replaceFirst("\\{[sdf]\\}", "%" + i + "$s");
i++;
}
I'm getting IllegalArgumentException: Illegal group reference
while executing replaceFirst
. Why?
replacement
part in replaceFirst(regex,replacement)
can contain references to groups matched by regex
. To do this it is using
$x
syntax where x
is integer representing group number, ${name}
where name
is name of named group (?<name>...)
Because of this ability $
is treated as special character in replacement
, so if you want to make $
literal you need to
\
like replaceFirst(regex,"\\$whatever")
Matcher
escape it for you using Matcher.quote
method replaceFirst(regex,Matcher.quote("$whatever"))
BUT you shouldn't be using
value = value.replaceFirst("\\{[sdf]\\}", "%" + i + "\\$s");
inside loop because each time you do, you need to traverse entire string to find part you want to replace, so each time you need to start from beginning which is very inefficient.
Regex engine have solution for this inefficiency in form of matcher.appendReplacement(StringBuffer, replacement)
and matcher.appendTail(StringBuffer)
.
appendReplacement
method is adding to StringBuffer all data until current match, and lets you specify what should be put in place of matched by regex partappendTail
adds part which exists after last matched partSo your code should look more like
StringBuffer sb = new StringBuffer();
int i = 0;
Matcher tagsMatcher = Pattern.compile("\\{[sdf]\\}").matcher(value);
while (tagsMatcher.find()) {
tagsMatcher.appendReplacement(sb, Matcher.quoteReplacement("%" + (i++) + "$s"));
}
value = sb.toString();
You need to escape the dollar symbol.
value = value.replaceFirst("\\{[sdf]\\}", "%" + i + "\\$s");
Illegal group reference error occurs mainly because of trying to refer a group which really won't exists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With