如何匹配python中单词首次出现后的部分

如何匹配python中单词首次出现后的部分,python,regex,Python,Regex,例如,输入字符串: In North Dighton, there's a Flash Flood Watch in effect until Wednesday, July 12, 9:00 PM. 我想提取以下字符串 North Dighton 因此,我编写了如下python代码: found_group = re.search('(.*)in (.*?),(.*)', "In North Dighton, there's a Flash Flood Watch in effect un

例如,输入字符串:

In North Dighton, there's a Flash Flood Watch in effect until Wednesday, July 12, 9:00 PM.
我想提取以下字符串

North Dighton
因此,我编写了如下python代码:

found_group = re.search('(.*)in (.*?),(.*)', "In North Dighton, there's a Flash Flood Watch in effect until Wednesday, July 12, 9:00 PM.", re.IGNORECASE)

fround_group.group(2)
然而,它输出:

effect until Wednesday
如何仅匹配第一个“in”和第一个逗号之间的部分


注意,第一个“in”可能不是该行的第一个单词。

必须使用否定字符类,以不匹配
,而匹配任何字符。因为正则表达式中的
*
是贪婪的,在找到匹配项之前,它不会吃掉任何字符,或者使
*
*?

found_group = re.search('([^,]*)in ([^,]*),(.*)', "In North Dighton, there's a Flash Flood Watch in effect until Wednesday, July 12, 9:00 PM.", re.IGNORECASE)
fround_group.group(2)

t=“在北迪格顿,有一个有效的山洪观测,直到7月12日星期三晚上9:00。”

o/p


“North Dighton”

我甚至看到了这样的效果:在(.*),(.*)“North Dighton有一个闪光洪水监测,有效期到7月12日星期三晚上9:00。”,re.IGNORECASE)
import re
re.search(r'In (.+?),', t).group(1)