Trying to update some latex code. The four lines:
something $^\text{TT}$ and more stuff
something $^\text{T}$ and more stuff
something $^\text{M\times N}$ and more stuff
something $^\text{T} {otherstuff}$
should turn into
something $^{\text{TT}}$ and more stuff
something $^{\text{T}}$ and more stuff
something $^{\text{M\times N}}$ and more stuff
something $^{\text{T}} {otherstuff}$
In other words, encase the super-script by an extra {...}
.
My attempt at using sed
is the following
sed 's/\^\\text{\(.*\)}/\^{\\{text{\1}}/' testregex
This works on the first three lines, but the final line doesn't work and produces
something $^{\text{T} {otherstuff}}$
instead. The problem is that sed
is matching with the last }
on each line, but I need it to match the first }
after the \text{
Also, it would be great if this could work multiple times on the same line, for example,
^\text{1} la la la ^\text{2}
should turn into
^{\text{1}} la la la ^{\text{2}}
I'm sure there's just one tiny little modification to make it work, but I can't figure it out and it's driving me nuts. Thanks in advance!
Best Answer
One method to overcome the greedy regular expression problem is to explicitly look for a string of non-delimiter characters, followed by the delimiter character. At the same time, you can probably simplify your replacement syntax via:
It should be possible to use
for multiple matches per line.