gpt4 book ai didi

Python 无法编译正则表达式

转载 作者:太空宇宙 更新时间:2023-11-03 16:10:47 25 4
gpt4 key购买 nike

我正在尝试使用 python 正则表达式从 cmake 文件中检测所有 set,如下文件:

# Library to include
set(LIB_TO_INCLUDE
a
b
c)

# comon code (inclusion in source code)
set(SHARED_TO_INCLUDE d e f)

# Library to include
set(THIRD_PARTY g h)

我想检索:

LIB_TO_INCLUDE a b c
SHARED_TO_INCLUDE d e f
THIRD_PARTY g h

我测试了正则表达式 set\((?s:[^)])*?\) (获取除 ) 后面的所有项目 set()使用 regex101.com (参见 https://regex101.com/r/aB5tX2/1 ),它显然做了我想要的。

现在,当我尝试从 Python 运行 re.compile(r'set\((?s:[^)])*?\)') 时,出现错误:

  File "private\python_scripts\convert.py", line 34, in create_sde_files
pattern = re.compile(r'set\((?s:[^)])*?\)') File "b:\dev\vobs_ext_2015\tools_ext\python\Python34_light\lib\re.py", line 223, in compile
return _compile(pattern, flags) File "b:\dev\vobs_ext_2015\tools_ext\python\Python34_light\lib\re.py", line 294, in _compile
p = sre_compile.compile(pattern, flags) File "b:\dev\vobs_ext_2015\tools_ext\python\Python34_light\lib\sre_compile.py", line 568, in compile
p = sre_parse.parse(p, flags) File "b:\dev\vobs_ext_2015\tools_ext\python\Python34_light\lib\sre_parse.py", line 760, in parse
p = _parse_sub(source, pattern, 0) File "b:\dev\vobs_ext_2015\tools_ext\python\Python34_light\lib\sre_parse.py", line 370, in _parse_sub
itemsappend(_parse(source, state)) File "b:\dev\vobs_ext_2015\tools_ext\python\Python34_light\lib\sre_parse.py", line 721, in _parse
raise error("unknown extension") sre_constants.error: unknown extension

Python不支持这种正则表达式吗?

最佳答案

这应该可以做到:set\(([^)]*?)\)

编译正则表达式时,“单行”修饰符将作为参数传递:

>>> t = """set(LIB_TO_INCLUDE 
... a
... b
... c)"""
>>>
>>> pattern = r'set\(([^)]*?)\)'
>>>
>>> regex = re.compile(pattern, re.S)
>>>
>>> result = regex.search(t).groups()[0]
>>> result
'LIB_TO_INCLUDE \n a\n b\n c'

然后您可以消除额外的间距和新行:

>>> ' '.join(x.strip() for x in result.split('\n'))
'LIB_TO_INCLUDE a b c'

请注意,在您的链接中,如果您在左侧的“ flavor ”中切换到“python”,您将收到特定格式导致的错误。

编辑:要获取所有 (3) 个匹配项,您需要使用 <regex>.findall(...)而不是search .

>>> tt = """# Library to include
... set(LIB_TO_INCLUDE
... a
... b
... c)
...
... # comon code (inclusion in source code)
... set(SHARED_TO_INCLUDE d e f)
...
... # Library to include
... set(THIRD_PARTY g h)"""
>>>

>>> result = regex.findall(tt)
>>> result
['LIB_TO_INCLUDE \n a\n b\n c', 'SHARED_TO_INCLUDE d e f', 'THIRD_PARTY g h']

关于Python 无法编译正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39328878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com