首页 > 解决方案 > 正则表达式python匹配单词与`est`

问题描述

嗨,我正在尝试匹配包含est句子中的单词。

例如字符串是:

You are the best
greatest show of all time

我想要的输出是

best
greatest

到目前为止,这是我的正则表达式代码

(?:^|)(est|est)(?:$|)

关于如何实施它的任何想法?

标签: pythonregex

解决方案


我会re.findall用这种模式\b\w*est\w*\b

inp = """You are the best
greatest show of all time"""
matches = re.findall(r'\b\w*est\w*\b', inp)
print(matches)

这打印:

['best', 'greatest']

正则表达式模式查找包含 g 的所有单词est,允许在之前或之后的任意数量的单词字符est


推荐阅读