0

I am trying to write a regular expression for a find and replace, using three capture groups:

({\\textless}sub{\\textgreater})(.*)({\\textless}\/sub{\\textgreater})

This works perfectly on this string:

C{\textless}sub{\textgreater}3{\textless}/sub{\textgreater}

Allowing me to replace group 1 with $_, preserve group 2 (using \2), and replace group 3 with $, such that my string becomes C$_3$.

But it doesn't work on this string where the pattern is repeated in the same line:

\textless}sub{\textgreater}3{\textless}/sub{\textgreater} and C{\textless}sub{\textgreater}4{\textless}/sub{\textgreater}

Because the (.*) matches everything including the next start of group 1. See here: https://regex101.com/r/rT31ax/1

I.e. what I get from the replace is:

C$_3{\textless}/sub{\textgreater} and C{\textless}sub{\textgreater}4$

But what I want is:

C$_3$ and C$_4$

How can I adjust the (.*) so that it matches anything EXCEPT what is in group 1?

0 Answers0