1. Two-step approach
import re
subject = '''
This "TODO" is not within a comment, but the next one is.
<!--
TODO
: Come up with a cooler comment for this example.
-->
'''
list = []
innerre = re.compile(r"\bTODO\b")
for outermatch in re.finditer("<!--([\s\S]*?)-->", subject):
list.extend(innerre.findall(outermatch.group(1)))
print list
2. Single-step approach
import re
subject = '''
This "TODO" is not within a comment, but the next one is.
<!--
TODO
: Come up with a cooler comment for this example.
-->
'''
match = re.search(r"\bTODO\b(?=(?:(?!<!--)[\s\S])*?-->)", subject, re.DOTALL)
if match:
result = match.group()
else:
result = ""
print result
Python 正则表达式查找XML注释中的特定词
最新推荐文章于 2023-12-04 09:20:40 发布