Python重新“假转义错误" [英] Python re "bogus escape error"

查看:39
本文介绍了Python重新“假转义错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 python re modules .search 方法.cur 是来自 Tkinter 条目小部件的输入.每当我在条目小部件中输入\"时,它都会引发此错误.我并不完全确定错误是什么或如何处理它.任何见解将不胜感激.

cur 是一个字符串

tup[0] 也是一个字符串

片段:

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

错误:

Tkinter 回调中的异常回溯(最近一次调用最后一次):文件C:\Python26\Lib\Tkinter.py",第 1410 行,在 __call__ 中返回 self.func(*args)文件C:\Python26\Suite\quidgets7.py",第 2874 行,在 quick_links_results 中self.quick_links_results_s()文件C:\Python26\Suite\quidgets7.py",第 2893 行,在 quick_links_results_sse = re.search(cur, tup[0], flags=re.IGNORECASE)文件C:\Python26\Lib\re.py",第 142 行,在搜索中返回 _compile(pattern, flags).search(string)_compile 中的文件C:\Python26\Lib\re.py",第 245 行引发错误,v # 无效的表达式错误:假转义(行尾)

解决方案

"bogus escape (end of line)" 意味着您的模式以反斜杠结尾.这与 Tkinter 无关.您可以在交互式 shell 中轻松复制错误:

<预><代码>>>>进口重新>>>模式=foobar\\">>>研究(模式,foobar")回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py",第 142 行,在搜索中返回 _compile(pattern, flags).search(string)文件/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py",第 241 行,在 _compile引发错误,v # 无效的表达式sre_constants.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)

解决方案

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

这篇关于Python重新“假转义错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆