Consider the following line:
He ordered pizza with anchovies. Unfortunately, this was not what he wanted. In addition, pizza with mushrooms, pepperoni and anchovies is much better than regular pizza with anchovies.
Say what you need to change pizza with (ingredients)
to pizza with (ingredients) on a thin crust
.
To do this, I installed Regex:
(?i:pizza with [a-zA-Z,\s]*?anchovies)
It captures three matches. Then I continue to add on a thin crust
to each match using the following code:
Pattern p = Pattern.compile("(?i:pizza with [a-zA-Z,\s]*?anchovies)");
Matcher m = p.matcher(string);
while(m.find())
{
string = string.replace(m.group(), m.group() + "on a thin crust.");
}
The result of this will be:
He ordered pizza with anchovies on a thin crust on a thin crust. Unfortunately, this was not what he wanted. In addition, pizza with mushrooms, pepperoni and anchovies is much better than regular pizza with thin crust anchovies and thin crust.
What happened:
pizza with anchovies
. , String.replace
pizza with anchovies on a thin crust
. , , replace ( ). , double on a thin crust
.
:
Regex ?