Need simple regex for LaTeX
latex, regex
Solution
sed:
s/\$\\displaystyle({[^}]+})\$/\\mymath\1/g
Problem
In my LaTeX files, I have literally thousands of occurrences of the following construct: ``` $\displaystyle{...math goes here...}$ ``` I'd like to replace these with ``` \mymath{...math goes here...} ``` Note that the $'s disappear, but the curly braces remain---if not for the trailing $, this would be a basic find-and-replace. If only I knew any regex, I'm sure it would handle this with no problem. What's the regex I need to make this happen? Many thanks in advance. Edit: Some issues and questions have arisen, so let me clarify: - Yes, `$\displaystyle{ ... }$` can occur multiple times on the same line. - No, nested `}$`'s (such as `$\displaystyle{...{more math}$...}$`) cannot occur. I mean, I suppose it could if you put it in an `\mbox` or something, but I can't imagine why anyone would ever do that inside a `$\displaystlye{}$` construct, the purpose of which is to display math inline with text. At any rate, it's not something I've ever done or am likely to do. - I tried using the perl suggestion, but while the shell raised no objections, the files remained unaffected. - I tried using the sed suggestion, but the shell objected to an "unexpected token near `('". I've never used sed before (and "man sed" was obtuse), but here's what I did: navigated to a directory containing .tex files and typed "`sed s/\$\\displaystyle({[^}]+})\$/\\mymath\1/g *.tex`". No luck. How do I use sed to do what I want? Again, many many thanks for all offered help.