从3.5.4传递到3.6.8时,如何解决Python正则表达式错误“错误错误错误转义\m位于位置37”

2024-05-14 07:25:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一段在3.5.4上运行的python代码,但现在在3.6.8上被破坏了。它似乎与python如何管理正则表达式有关。找到了一些thread,但我不知道如何解决我的问题

我是否需要更改给定给regexr'^\s*#\s*include\s+"%s"' % str(moc_cpp[0])的字符串

堆栈跟踪:

error: bad escape \m at position 37:
  File "c:\python\python3.6.8\lib\site-packages\SCons\Builder.py", line 353:
    target, source = e(target, source, env)
  File "C:\SVN\3rdParty\devTool\site_scons\site_tools\qt5\__init__.py", line 373:
    cpp, cpp_contents, out_sources)
  File "C:\SVN\3rdParty\devTool\site_scons\site_tools\qt5\__init__.py", line 229:
    if cpp and re.search(inc_moc_cpp, cpp_contents, re.M):
  File "c:\python\python3.6.8\lib\re.py", line 182:
    return _compile(pattern, flags).search(string)
  File "c:\python\python3.6.8\lib\re.py", line 301:
    p = sre_compile.compile(pattern, flags)
  File "c:\python\python3.6.8\lib\sre_compile.py", line 562:
    p = sre_parse.parse(p, flags)
  File "c:\python\python3.6.8\lib\sre_parse.py", line 855:
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
  File "c:\python\python3.6.8\lib\sre_parse.py", line 416:
    not nested and not items))
  File "c:\python\python3.6.8\lib\sre_parse.py", line 502:
    code = _escape(source, this, state)
  File "c:\python\python3.6.8\lib\sre_parse.py", line 401:
    raise source.error("bad escape %s" % escape, len(escape))

代码:

        inc_moc_cpp = _contents_regex(r'^\s*#\s*include\s+"%s"' % str(moc_cpp[0]))
        if cpp and re.search(inc_moc_cpp, cpp_contents, re.M): 

def _contents_regex(e):
    # get_contents() of scons nodes returns a binary buffer, so we convert the regexes also to binary here
    # this won't work for specific encodings like UTF-16, but most of the time we will be fine here.
    # note that the regexes used here are always pure ascii, so we don't have an issue here.
    if sys.version_info.major >= 3:
        e = e.encode('ascii')
    return e

Tags: pyresourceparseliblinecontentssite
1条回答
网友
1楼 · 发布于 2024-05-14 07:25:28

moc_cpp[0]可能包含无效的转义序列。如果您打算逐字匹配它,而不是作为一个regexp,那么您应该在替换到regexp之前对其进行转义

inc_moc_cpp = _contents_regex(r'^\s*#\s*include\s+"%s"' % re.escape(str(moc_cpp[0])))

相关问题 更多 >

    热门问题