提问者:小点点

在python中使用正则表达式提取子字符串后面的字符


我有一根长得像这样的绳子-

text = 'during the day, the color of the sky is blue. at sunset, the color of the sky is orange.'

我需要提取特定子字符串后面的单词,在本例中是'skyis'。 也就是说,我想要一份清单,上面写着-

['blue', 'orange']

我试过以下方法-

p1 =re.compile(r"is (.+?) ",re.I)
re.findall(p1,text)

但这只给出如下输出

['blue']

但是,如果我的文本是

text = 'during the day, the color of the sky is blue at sunset, the color of the sky is orange or yellow.'

我跑了

p1 = re.compile(r"is (.+?) ",re.I)
re.findall(p1,text)

我得到的输出是-

['blue', 'orange']

请救命! 我是正则表达式的新手,我被卡住了!


共2个答案

匿名用户

这不是一个非常通用的解决方案,但它适用于您的字符串。

my_str = 'during the day, the color of the sky is blue. at sunset, the color of the sky is orange.'
r = re.compile('sky is [a-z]+')
out = [x.split()[-1] for x in r.findall(my_str)]

匿名用户

在regex模式中,您只捕获后跟空格的字符串,但是“orange”后跟点“。”,这就是它没有被捕获的原因。
您必须包含点“。”。 按照你的模式。

re.compile(r"is (.+?)( |\.)", re.I)

演示:
https://regex101.com/r/b8jhdf/1