[Tex/LaTex] How to insert submatches from regular expression in TexStudio

texstudio

I am trying to use the regular expression feature from TexStudio but somehow I can't get it to work and i couldn't find an example or an explanation in the manual.

I am trying to replace variables y1,y2,y3 (y1) with x_1,x_2,x_3 (x_1). In the tip from 'Reg' it is mentioned that 'You can use \1 to \9 in the replace text to insert a submatch.'

enter image description here

I tried to Replace it like this but now everything is replaced with (x_\1) and not the corresponding number. What am I doing wrong?

In adddition: is there a regular expression for all numbers? (1 as well as 1999) For letters there is [A-Z], but [0-9999] only gets the first digit marked.

Thank You!

Best Answer

You need to use capture groups in order for the \1 to work.

Find: y(\d+)
Replace: x_\1

should work. You can check what is being replaced with the little preview text after the \1 button.

Also, \d characters are digits, then + just means 1 or more.

enter image description here

Related Question