Java replace all capturing groups
- by Don
Hi,
If I have a regex with a capturing group, e.g. foo(g.*f). If I match this against a string and want to replace the first capturing group in all matches with baz so that
foog___f blah foog________f
is converted to:
foobaz blah foobaz
There doesn't appear to be any easy way to do this using the standard libraries, because the Matcher.replaceAll() method will only replace all matches of the entire pattern, am I missing something?
Obviously I can just iterate through the matches, store the start and end index of each capturing group, then go back and replace them, but is there an easier way?
Thanks,
Don