我正在编写一个Python脚本,用于在文本中查找短语(1到5个单词)。 我想让其他单词成为我正在寻找的短语之一,我试图使用正则表达式来完成这样的任务。
说我的短语是
p = "red blue green"
我希望JUST ONE允许任何其他单词出现在其中并仍然匹配:
t1 = "this is a red blue green flower"
应该是匹配
t2 = "this is a red blue yellow green flower"
应该是匹配
t3 = "this is a red violet blue yellow green flower"
应该是匹配
t4 = "this is a red blue yellow and green flower"
不应该匹配
这是哪个正则表达式?
答案 0 :(得分:0)
如果始终遵守订单red, blue, green
,这是获取相应正则表达式模式的方法:
markers = "red blue green".split()
pattern = r'\b'+ ' (\w+ )?'.join(markers) + r'\b'
print(pattern)
# \bred (\w+ )?blue (\w+ )?green\b
这是一个小测试:
t1 = "this is a red blue green flower"
t2 = "this is a red blue yellow green flower"
t3 = "this is a red violet blue yellow green flower"
t4 = "this is a red blue yellow and green flower"
import re
print(re.search(pattern, t1))
# <_sre.SRE_Match object at 0x7ff0a72741c8>
print(re.search(pattern, t2))
# <_sre.SRE_Match object at 0x7ff0a72741c8>
print(re.search(pattern, t3))
# <_sre.SRE_Match object at 0x7ff0a72741c8>
print(re.search(pattern, t4))
# None