正则表达式匹配两个字符串?

Hrv*_*jar 18 python regex regex-negation python-3.x

我似乎找不到像以下示例中提取所有注释的方法.

>>> import re
>>> string = '''
... <!-- one 
... -->
... <!-- two -- -- -->
... <!-- three -->
... '''
>>> m = re.findall ( '<!--([^\(-->)]+)-->', string, re.MULTILINE)
>>> m
[' one \n', ' three ']
Run Code Online (Sandbox Code Playgroud)

two -- --由于正则表达式错误,阻止与最不匹配.有人可以指出我正确的方向如何提取两个字符串之间的匹配.


嗨,我已经测试了你们在评论中建议的内容......这里是工作解决方案,几乎没有升级.

>>> m = re.findall ( '<!--(.*?)-->', string, re.MULTILINE)
>>> m
[' two -- -- ', ' three ']
>>> m = re.findall ( '<!--(.*\n?)-->', string, re.MULTILINE)
>>> m
[' one \n', ' two -- -- ', ' three ']
Run Code Online (Sandbox Code Playgroud)

谢谢!

iru*_*var 38

这应该可以解决问题

 m = re.findall ( '<!--(.*?)-->', string, re.DOTALL)
Run Code Online (Sandbox Code Playgroud)