We can find a pattern and replace it with some text and the replaced text is depending on the matched text.
In Java we can use the following two methods in the Matcher class to accomplish this task.
Matcher appendReplacement(StringBuffer sb, String replacement) StringBuffer appendTail(StringBuffer sb)
Suppose we have the following text.
We would like to change the number to text in the following rules.
After replacing, the above sentence would be
To find all numbers, we can use
\b marks the word boundaries.
The following code shows how to use Regular Expressions and appendReplacement() and appendTail() Methods
The code above generates the following result.