Matt G Matt G - 15 days ago 7
Python Question

Regexp in python, lost in backslash

I have this pattern, and this windows filename I want to match it to. Names changed to protect the innocent: [a-zA-Z]+ was replaced with successive letters of the alphabet.

pattern = "C:\\\\a\\\\b\\\\c\\\\d\\\\e f\\\\(g|h|i)\\\\j k\\\\l\\\\m\\\\(n\\\\(o|p)|q)\\\\.*\.sql)"

str = "C:\\a\\b\\c\\d\\e f\\h\\j k\\l\\m\\n\\o\\z_zz.function.sql"


But, python complains about an unmatched paren. I don't know why. The position it gives me (62) also doesn't line up with any of my parens, but if I had to guess I'd say one of the many backslashes is accidentally escaping (g or (o when I didn't want it to, but adding more backslashes in front of those hasn't helped.

Answer

Your backslashes are all properly escaped. The position it gives (62) does match up with a paren, the last one to be specific:

>>> pattern = "C:\\\\a\\\\b\\\\c\\\\d\\\\e f\\\\(g|h|i)\\\\j k\\\\l\\\\m\\\\(n\\\\(o|p)|q)\\\\.*\.sql)"
>>> pattern[62]
')'
>>> len(pattern)
63

So match that right paren with a left paren where you want your group to be, or remove the last paren from you pattern.