[英]Regex: match text between 2 items
我將如何匹配以下代碼以獲取兩個字符串:
這是要匹配的字符串:
title
<a></a>
content here
<a></a>
text...
<a></a>
text...
title
<a></a>
<a></a>
<a></a>
我嘗試使用。*,但這從標題到最后一個標簽捕獲了文本。
from re import findall, DOTALL
text = '''
title
<a></a>
content here
<a></a>
text...
<a></a>
text...
title
<a></a>
<a></a>
<a></a>
'''
print findall(r'title.*?</a>.*?</a>.*?</a>', text, DOTALL)
給
['title\n<a></a>\ncontent here\n<a></a>\ntext...\n<a></a>', 'title \n<a></a>\n<a></a>\n<a></a>']
你也可以使用
print findall(r'title(?:.*?</a>){3}', text, DOTALL)
通常*
是貪婪的,而*?
不情願。 嘗試將.*
替換為.*?
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.