Python re "false exit error"

I was messing around with the python re modules .search module . cur is the input from the Tkinter login widget. Whenever I enter "\" in the input widget, it throws this error. I'm not quite sure what a mistake is or how to deal with it. Any insight would be greatly appreciated.

cur - string

tup [0] is also a string

Excerpt:

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

Mistake:

 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) 
+7
python regex tkinter
Dec 13 '10 at 8:59
source share
4 answers

"dummy exit (end of line)" means that your pattern ends with a backslash. This has nothing to do with Tkinter. You can easily copy the error in the 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) 

Decision? Make sure your pattern does not end with a single backslash.

+12
Dec 13 2018-10-12
source share

The solution to this problem is to use the raw string as replacement text. The following actions will not be performed:

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

It will throw an error: dummy output (end of line)

But the following will work just fine:

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

Now the question is how to convert the string generated at run time to the original string in Python. You can find a solution for this here . But I prefer to use a simpler method for 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 only convert ascii and unicode strings to raw strings. Well, this has worked great for me so far :)

+12
Jan 01 '11 at 9:59
source share

The first parameter for re is the search pattern, so if 'cur' contains a backslash at the end of the line, it will be an invalid escape sequence. You probably changed your arguments (I don't know what tup [0] is, but is this your template?), And it should be like

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

Since you very rarely use user input as a template (if you do not follow the regex search mechanism, in this case you may need to show an error).

NTN.

EDIT :
The error reported is that you use the escape character to the end of the line (which means bogus escape (end of line) ), i.e. your pattern ends with a backslash, which is not a valid pattern. The Escape character (backslash) must be followed by another character that removes or adds special meaning to this character (not sure exactly how python works, posix does groups by adding escape in parentheses, perl removes the group effect, avoiding it). That is, \* corresponds to literal asterics, while * corresponds to the previous character 0 or more times.

+3
Dec 13 '10 at 9:05
source share

If you are trying to find "cur" in "tup [0]", you must do this via the "try: ... except: ..." block to catch an 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. 
+3
Dec 13 '10 at 9:27
source share



All Articles