【问题标题】:Regular expression working in Python3.5.2, not 3.7.4正则表达式在 Python3.5.2 中工作,而不是 3.7.4
【发布时间】:2020-03-11 20:24:38
【问题描述】:

我正在使用我在网上找到的修改后的库来解析 .stl 文件。到目前为止,它在非 Anaconda Python 3.5.2 中运行良好。我最近不得不升级到 Anaconda Python3.7.4。以下行在 3.5 中运行良好,但在 3.7.4 中抛出异常

re.compile(r'[-+]?[0-9]*\.?[0-9]+(\e[-+]?[0-9]+)?')

这是什么原因?
例外是:

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-2-08df04adea1a> in <module>
----> 1 re.compile(r'[-+]?[0-9]*\.?[0-9]+(\e[-+]?[0-9]+)?')

~/anaconda3/lib/python3.7/re.py in compile(pattern, flags)
    232 def compile(pattern, flags=0):
    233     "Compile a regular expression pattern, returning a Pattern object."
--> 234     return _compile(pattern, flags)
    235 
    236 def purge():

~/anaconda3/lib/python3.7/re.py in _compile(pattern, flags)
    284     if not sre_compile.isstring(pattern):
    285         raise TypeError("first argument must be string or compiled pattern")
--> 286     p = sre_compile.compile(pattern, flags)
    287     if not (flags & DEBUG):
    288         if len(_cache) >= _MAXCACHE:

~/anaconda3/lib/python3.7/sre_compile.py in compile(p, flags)
    762     if isstring(p):
    763         pattern = p
--> 764         p = sre_parse.parse(p, flags)
    765     else:
    766         pattern = None

~/anaconda3/lib/python3.7/sre_parse.py in parse(str, flags, pattern)
    928 
    929     try:
--> 930         p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
    931     except Verbose:
    932         # the VERBOSE flag was switched on inside the pattern.  to be

~/anaconda3/lib/python3.7/sre_parse.py in _parse_sub(source, state, verbose, nested)
    424     while True:
    425         itemsappend(_parse(source, state, verbose, nested + 1,
--> 426                            not nested and not items))
    427         if not sourcematch("|"):
    428             break

~/anaconda3/lib/python3.7/sre_parse.py in _parse(source, state, verbose, nested, first)
    814             sub_verbose = ((verbose or (add_flags & SRE_FLAG_VERBOSE)) and
    815                            not (del_flags & SRE_FLAG_VERBOSE))
--> 816             p = _parse_sub(source, state, sub_verbose, nested + 1)
    817             if not source.match(")"):
    818                 raise source.error("missing ), unterminated subpattern",

~/anaconda3/lib/python3.7/sre_parse.py in _parse_sub(source, state, verbose, nested)
    424     while True:
    425         itemsappend(_parse(source, state, verbose, nested + 1,
--> 426                            not nested and not items))
    427         if not sourcematch("|"):
    428             break

~/anaconda3/lib/python3.7/sre_parse.py in _parse(source, state, verbose, nested, first)
    505 
    506         if this[0] == "\\":
--> 507             code = _escape(source, this, state)
    508             subpatternappend(code)
    509 

~/anaconda3/lib/python3.7/sre_parse.py in _escape(source, escape, state)
    400         if len(escape) == 2:
    401             if c in ASCIILETTERS:
--> 402                 raise source.error("bad escape %s" % escape, len(escape))
    403             return LITERAL, ord(escape[1])
    404     except ValueError:

error: bad escape \e at position 21

【问题讨论】:

  • 我不确定。我在更改日志中看不到任何表明确实如此的内容。我在忘记提及的问题中添加的一件事是我也从非 Anaconda 更改为 Anaconda Python。我不知道这是否会改变什么。
  • 如果将\e 更改为e 会发生什么?
  • 哇,突然就可以了。你能发布一个答案来解释发生了什么以及这是什么原因吗?这是一个错误吗?

标签: python regex python-3.5 python-3.7


【解决方案1】:

您遇到的问题是由于您添加了\e 的转义序列。将其更改为 e 将解决您的问题。

那么为什么在你从 python 3.5.2 升级到 python 3.7.4 之后会发生这种情况?

感谢 Wiktor 在this question 下的评论中指出这一点,为我指明了正确的方向。

根据What's new in Python 3.7 - API and Feature Removals

\ 和替换的ASCII 字母组成的未知转义 re.sub() 的模板在 Python 3.5 中已弃用,现在将 导致错误

也在3.7 - Regular Expression Syntax(该部分的最后一行)中注明:

在 3.6 版更改:由 '\' 和 ASCII 组成的未知转义 这封信现在是错误的。


您还可以在此处查看具有类似信息的另一个答案(由 Wiktor 提供):The “\z” anchor not working in Python regex

【讨论】:

猜你喜欢
  • 2015-08-10
  • 1970-01-01
  • 2016-08-01
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多