首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正则表达式在Python3.5.2中工作,而不是3.7.4

正则表达式在Python3.5.2中工作,而不是3.7.4
EN

Stack Overflow用户
提问于 2019-11-15 16:31:32
回答 1查看 636关注 0票数 3

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

代码语言:javascript
复制
re.compile(r'[-+]?[0-9]*\.?[0-9]+(\e[-+]?[0-9]+)?')

原因是什么?

例外是:

代码语言:javascript
复制
---------------------------------------------------------------------------
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-15 16:55:26

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

那么,在您从python 3.5.2升级到python 3.7.4之后,为什么会出现这种情况呢?

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

根据Python3.7-API和特性删除中的新内容

\re.sub()替换模板中的ASCII字母组成的未知转义符在Python3.5中被弃用,现在将导致错误

3.7 -正则表达式语法中也提到了(这一节的最后一行):

在版本3.6中更改:由'\‘和ASCII字母组成的未知转义符现在都是错误。

您还可以在这里看到另一个具有类似信息的答案(由Wiktor提供):Python regex中不工作的“\z”锚

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58881151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档