开发者

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\Tki开发者_StackOverflow中文版nter.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)


"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.


The solution to this issue is to use a raw string as the replacement text. The following won't work:

re.sub('this', 'This \\', 'this is a text')

It will throw the error: bogus escape (end of line)

But the following will work just fine:

re.sub('this', r'This \\', 'this is a text')

Now, the question is how do you convert a string generated during program runtime into a raw string in Python. You can find a solution for this here. But I prefer using a simpler method to do this:

def raw_string(s):
    if isinstance(s, str):
        s = s.encode('string-escape')
    elif isinstance(s, unicode):
        s = s.encode('unicode-escape')
    return s

The above method can convert only ascii and unicode strings into raw strings. Well, this has been working great for me till date :)


If you are trying to search for "cur" in "tup[0]" you should do this through "try:... except:..." block to catch invalid pattern:

try :
    se = re.search(cur, tup[0], flags=re.IGNORECASE)
except re.error, e:
    # print to stdout or any status widget in your gui
    print "Your search pattern is not valid."
    # Some details for error:
    print e
    # Or some other code for default action.


The first parameter to re is the pattern to search for, thus if 'cur' contains a backslash at the end of the line, it'll be an invalid escape sequence. You've probably swapped your arguments around (I don't know what tup[0] is, but is it your pattern?) and it should be like this

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

As you very rarely use user input as a pattern (unless you're doing a regular expression search mechanism, in which case you might want to show the error instead).

HTH.

EDIT:
The error it is reporting is that you're using an escape character before the end of line (which is what bogus escape (end of line) means), that is your pattern ends with a backslash, which is not a valid pattern. Escape character (backslash) must be followed by another character, which removes or adds special meaning to that character (not sure exactly how python does it, posix makes groups by adding escape to parentheses, perl removes the group effect by escaping it). That is \* matches a literal asterix, whereas * matches the preceding character 0 or more times.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜