I'm using python 2.7.17 and am trying to do some regular expression manipulation. Everything was working up until I ran into the following error:
Traceback (most recent call last):
File "latex/latex.py", line 130, in <module>
contents = re.sub(re.escape(i),img,contents, 0, re.MULTILINE)
File "/usr/lib/python2.7/re.py", line 155, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "/usr/lib/python2.7/re.py", line 286, in _subx
template = _compile_repl(template, pattern)
File "/usr/lib/python2.7/re.py", line 273, in _compile_repl
raise error, v # invalid expression
sre_constants.error: missing group name
I don't know what's wrong because I am actually escaping the string before putting it into sub:
print "----"
print i
print re.escape(i)
print img
contents = re.sub(re.escape(i),img,contents, 0, re.MULTILINE)
Here is what the print statements produce:
----
$$n \in \Z_{\geq 0}$$
\$\$n\ \\in\ \\Z\_\{\\geq\ 0\}\$\$
<img class="latex inline" src="{filename}/images/latex-cache/symmetric-functions.md827a2321d2328298b1d5789840928039.png" /><!--n \in \Z_{\geq 0}-->
As you can see, it's properly escaping the string, but for some reason it's throwing the error. I don't know what the mistake is because all articles I can find (such as regex error : raise error, v # invalid expression ) don't escape the string.
Any help would be appreciated.
Edit
The contents of content is pulled from a file. I edited the script to also print the contents. Here is what I have in contents:
<div class="content">
Let $$n \in \Z_{\geq 0}$$
</div>
Edit 2
It looks like the error is coming from img. When I replace img with anything else, it works perfectly. I was able to narrow it down to it failing when:
img = "\g"
Am I supposed to be escaping img in some way?
I also tried:
img = "\\g"
and this also caused the same error.