1

I am trying to write a script that could convert things like \\[ or \\] to $$ in order to convert a MultiMarkdown document to Pandoc markdown document that could display equations in HTML. I am using Python to find all instances of these characters using

 matchstring=r'\\['
 re.sub(matchstring,'$$',content)

However I run into the following error:

unexpected end of regular expression:line 15:matchstring=re.compile(r'\\[')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 190:
return _compile(pattern, flags)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 245:
raise error, v # invalid expression

most likely because of the last [ I have in there. Does anyone know a way to get around this?

Shruti Kapoor
  • 1,106
  • 2
  • 12
  • 32

4 Answers4

1

Escape the [:

matchstring=re.compile(r'//\[')

Or better yet, use:

content.replace("//[", "$$")

and don't involve regex at all.

Mark
  • 106,305
  • 20
  • 172
  • 230
  • How would I go about multiple replace in the document? I have different patterns to match. Shoudl I be doing `content.replace("//[", "$$")` `content.replace("//]", "$$")` for all the patterns I want to match or is there a better way to do it? – Shruti Kapoor Mar 21 '13 at 18:53
  • Just realised that I could use `content.replace("//[", "$$")).replace(r"\\]", "$$")` to do multiple iterations of replace. Thanks @Igor! – Shruti Kapoor Mar 21 '13 at 23:26
1
pandoc -f markdown_mmd -t markdown

will do this for you! (For pandoc >= 1.11)

John MacFarlane
  • 8,511
  • 39
  • 33
0

Your problem is that you write r'//[', not r'\\[', but anyway try better this:

matchstring.replace(r'\\[', '$$').replace(r'\\]', '$$')
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
0

if you are using a "[" within your regex, I would assume it would be required to have an escape for it as it is used within regular expression.

try one of the following:

content = "testing123//]testing456"
matchstring = "//]"
result = content.replace(matchstring, "$$")
print result

or

content = "testing123//]testing456"
matchstring = '(//\])'
result = re.sub(matchstring,'$$',content)
print result

Either should work for your purpose.

AWainb
  • 868
  • 2
  • 13
  • 27
  • just saw the next comment.. if it is just the "]" characters that are to be replaced, then try: matchstring = "\]" – AWainb Mar 21 '13 at 18:27
  • the above comment was for the first example. For the second, use matchstring = '(\\])' – AWainb Mar 21 '13 at 18:39
  • To deal with both at the same time ("[" & "]"), try using something like this: re.sub('(\\]|\\[)','$$',content) – AWainb Mar 21 '13 at 18:43