I need to convert plain text files containing math with the dollar sign notation $myformula$ to the \(myformula\) notation. However I want the double dollar notation $$myformula$$ to not be replaced.
Up until now I was doing this by hand by finding an replacing all dollar signs with a space before them by the opening syntax \( and all other dollar signs by the close syntax \) and correcting the remaining mistakes by hand (like beginning of line, double dollar sings). I think a regex command I could use to convert multiple files via the Terminal would accellerate this a lot.
Here is an example how it looks and should look like:
Before:
This is $a+b_{k}+c$ sentence.
Another test $s_\text{hi}+3$.
$x=y$ there.
$$y=z$$ why?
$$z=z$$
This could be ($3^2$, $x_1$).
After:
This is \(a+b_{k}+c\) sentence. Another test \(s_\text{hi}+3\). \(x=y\) there. $$y=z$$ why? $$z=z$$ This could be (\(3^2\), \(x_1\)).
Thanks!
Regex: ([^\$])\$([^\$]+)\$([^\$])
Regex: (^|[^\$])\$([^\$]+)\$([^\$]|$)
Substitution: $1\\($2\\)$3
Pretty messy with 3 capture groups, but it seems to do exactly what is needed.
First capture anything that isn't $, followed by anything wrapped by $, followed by another character that isn't $, and replace it all with the second group wrapped in parenthesis, wrapped by the first and third group.
Edit: The first capture group should also check for ^, the beginning of the file, and the third group should check for $, the end of the file. The new Demo has examples that would break under my first statement.
Bad Demo
Good Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With