I have a long string. I want to replace all the matches with part of the matching regex (group).
For example:
String = "This is a great day, is it not? If there is something, THIS IS it. <b>is</b>".
I want to replace all the words "is" by, let’s say, "<h1>is</h1>". The case should remain the same as original. So the final string I want is:
This <h1>is</h1> a great day, <h1>is</h1> it not? If there <h1>is</h1> something,
THIS <h1>IS</h1> it. <b><h1>is</h1></b>.
The regex I was trying:
Pattern pattern = Pattern.compile("[.>, ](is)[.<, ]", Pattern.CASE_INSENSITIVE);
The
Matcherclass is commonly used in conjunction withPattern. Use theMatcher.replaceAll()method to replace all matches in the stringNote: Using the
\bregex command will match on a word boundary (like whitespace). This is helpful to use in order to ensure that only the word “is” is matched and not words that contain the letters “i” and “s” (like “island”).