Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python re "bogus escape error"

I've been messing around with the python re modules .search method. cur is the input from a Tkinter entry widget. Whenever I enter a "\" into the entry widget, it throws this error. I'm not all to sure what the error is or how to deal with it. Any insight would be much appreciated.

cur is a string

tup[0] is also a string

Snippet:

se = re.search(cur, tup[0], flags=re.IGNORECASE)

The error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python26\Lib\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Python26\Suite\quidgets7.py", line 2874, in quick_links_results
    self.quick_links_results_s()
  File "C:\Python26\Suite\quidgets7.py", line 2893, in quick_links_results_s
    se = re.search(cur, tup[0], flags=re.IGNORECASE)
  File "C:\Python26\Lib\re.py", line 142, in search
    return _compile(pattern, flags).search(string)
  File "C:\Python26\Lib\re.py", line 245, in _compile
    raise error, v # invalid expression
error: bogus escape (end of line)
like image 624
rectangletangle Avatar asked Dec 13 '10 08:12

rectangletangle


1 Answers

"bogus escape (end of line)" means that your pattern ends with a backslash. This has nothing to do with Tkinter. You can duplicate the error pretty easily in an interactive shell:

>>> import re
>>> pattern="foobar\\"
>>> re.search(pattern, "foobar")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py", line 142, in search
    return _compile(pattern, flags).search(string)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py", line 241, in _compile
    raise error, v # invalid expression
sre_constants.error: bogus escape (end of line)  

The solution? Make sure your pattern doesn't end with a single backslash.

like image 170
Bryan Oakley Avatar answered Oct 07 '22 00:10

Bryan Oakley