【发布时间】:2015-01-28 07:53:53
【问题描述】:
我正在使用 python 正则表达式进行一些正则表达式匹配。
pattern1 = re.compile('<a>(.*?)</a>[\s\S]*?<b>(.*?)</b>')
pattern2 = re.compile('<b>(.*?)</b>[\s\S]*?<a>(.*?)</a>')
items = re.findall(pattern1, line)
if items:
print items[0]
else:
items = re.findall(pattern2, line)
if items:
print items[0]
如您所见,标签 a 和 b 的顺序不是固定的(a 可以在 b 之前或之后)。
我使用了两种模式(先尝试模式 1,然后尝试模式 2)在标签 a 和标签 b 之间查找文本,但看起来很难看,但我不知道如何使用一种模式来获得与上述代码相同的结果。
谢谢!
【问题讨论】: