且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-10-16 13:33:58

这应该可以解决问题

 m = re.findall ( '', string, re.DOTALL)

I can't seem to find a way to extract all comments like in following example.

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

block with two -- -- is not matched most likely because of bad regex. Can someone please point me in right direction how to extract matches between two strings.


Hi I've tested what you guys suggested in comments.... here is working solution with little upgrade.

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

thanks!

this should do the trick

 m = re.findall ( '<!--(.*?)-->', string, re.DOTALL)