0

I am trying to write a short script to change tex syntax into rst syntax because in some instances, it's easier to just write $some math$ instead of :math:`some math`.

Now say I have a file that has multiple inline math occurences, e.g.

foo.rst:

Only $one$ inline math.
This $line$ has $some$ more $math$ then $the$ first one

So far, what I have working is the simple case where there is only one occurence (first line in example file), but sed seems to match the last $ in the line, not the first after the first match. This is my result:

 $ sed -E 's/\$(.+)\$/:math:`\1`/g' foo.rst
Only :math:`one` inline math.
This :math:`line$ has $some$ more $math$ then $others`

How do I need to change my sed command to match all $ pair-wise?

mivkov
  • 471
  • 5
  • 19
  • 1
    Use ``sed 's/\$\([^$]*\)\$/:math:`\1`/g' file > newfile`` – Wiktor Stribiżew Jun 13 '19 at 09:52
  • Ah, that seems to do the trick. Didn't know the 'greedy' terminology, so duplicate indeed. Thanks for the extremely quick reply! – mivkov Jun 13 '19 at 09:54
  • 1
    The point is to changed `.*` to a negated bracket expression `[^$]` that matches any char but `$`. It is working because the right-hand boundary is a single `$` char. If it is multicharacter, you would have to use Perl, or other work-arounds. – Wiktor Stribiżew Jun 13 '19 at 09:56

0 Answers0