正则表达式最小匹配

时间:2020-06-22 13:05:02

标签: python regex regex-greedy

我正在尝试使用Python编写一个正则表达式,以匹配包含特定单词的单个句子(从..)。 我写的正则表达式是r"\..*?hello.*?\."。如您所见,我正在使用非贪婪量词。 我要匹配的文本是:

This is the 1st sentence. This is the 2nd. This is the 3rd, hello, world. This is the 4th.

(这是regex101的link

我希望匹配为This is the 3rd, hello, world.,但是尽管*?量词是非贪婪的,但匹配项还包括第二句。我想念什么?

0 个答案:

没有答案