import re
text = "This sentence has phrase one and phrase word word two and phrase word three and phrase four phrase too many words too many words too many words four again."
l = [m[1] + ' ' + m[2] for m in re.finditer(r'\b(phrase)\b\W+(?:\w+\W+){0,6}?\b(one|two|three|four)\b', text)]
print(l)
正则表达式:
\b(phrase)\b
匹配单词边界上的phrase
李>W+
:匹配一个或多个非单词字符(通常是空格)李>(?:\w+\W+){0,6}?
匹配0到6次,尽可能少的匹配一个或多个单词字符,后跟一个或多个非单词字符李>\b(one|two|three|four)\b
匹配单词边界上的one
、two
、three
或four
李>代码:
印刷品:
相关问题 更多 >
编程相关推荐