search – Regexp searches that repeatedly cover expressions

I’m just going through an old Latex-text I’ve written and wanted to use regexp-replace to make any pair of brackets containing at least one other pair of brackets into a \left(.*\right)-expression. After some trying, I came up with this pair of regexp-replace expressions:

(\([^)]*?\)( → \\left(\1(
)\([^(]*?\)) → )\1\\right)

So the program should take any two most adjecent opening brackets that have between them no closing bracket and put a \left before the first bracket, and similar for closing brackets. However, there seems to be the problem that, if in such an expression as the first above regexp the second opening bracket starts a balanced expression that again contains other brackets, it still isn’t considered as the first opening bracket of another such regexp because it is already in one regexp. Thus, for instance, the following line

S( (Id(A)_{γ} Id(B) )_{↑ δ} Id(C) )

is transformed to the following expression after applying the two commands:

S\left( (Id(A)_{γ} Id(B) \right)_{↑ δ} Id(C) \right)

As one can see, there are two \right inserted but only one \left because the second opening bracket is already covered the first time the “insert-\left”-regexp is applied, whereas the dual statement is not true for the closing brackets because there is one pair of brackets between the last bracket and the other bracket closing an expression that contains other brackets. So an additional \left should be inserted before the second opening bracket of the statement.

Is there a way to avoid this behavior? So that the second opening bracket of the above line is tested again on whether or not it is the first bracket in a regexp such as the “insert-\left”-regexp?

Read more here: Source link