How can I search and replace in a region of a (TeX)buffer all occurences of \macro{foo}_{bar} to \macro{foo_{bar}}, foo and bar not being "constants" ?
Asked
Active
Viewed 10 times
1
cjorssen
- 197
- 6
1 Answers
1
Use regexp replacement (M-x replace-regexp or C-M-%) to replace a pattern with holes.
Assuming that foo and bar do not themselves contain braces or line breaks, replace \\macro{\(.*?\)}_{\(.*?\)} by \\macro{\1_{\2}}. Explanations (see the manual for more details):
\is a special character, so you need to write\\to match one backslash.\(…\)is a group: Emacs will remember what the group matched..*?matches any sequence of characters except a line break. It stops as early as possible (whereas.*would stop as late as possible, and could thus end up matching all of\macro{foo}_{bar} and more \stuff{baz}).- In the replacement,
\1and\2are replaced by what the corresponding group matched.
Gilles 'SO- stop being evil'
- 21,702
- 4
- 53
- 89
-
Works like a charm. Thank you so much. – cjorssen Nov 15 '22 at 21:07