簡體   English   中英

使用正則表達式在 python 中的 substring 之后提取字符

[英]Use regex to extract characters after a substring in python

我有一個看起來像這樣的字符串 -

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

我需要在特定子字符串之后提取單詞,在這種情況下,“天空是”。 也就是說,我想要一個給我這個的列表 -

['blue', 'orange']

我已經嘗試了以下 -

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

但這僅給出了 output

['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)

我得到 output 作為 -

['blue', 'orange']

請幫忙! 我是正則表達式的新手,我被卡住了!

這不是一個非常通用的解決方案,但它適用於您的字符串。

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)]

在你的正則表達式模式中,你只捕獲后面跟着空格的字符串,但是'orange'后面跟着一個點'.',這就是它沒有被捕獲的原因。
你必須包括點“。” 在你的模式中。

p1 = re.compile(r"is (.+?)[ \.]", re.I)
re.findall(p1,text)
# ['blue', 'orange']

演示:
https://regex101.com/r/B8jhdF/2

編輯:
如果單詞在句末並且后面沒有點“.”,我建議這樣做:

text = 'during the day, the color of the sky is blue at sunset, the color of the sky is orange'
p1 = re.compile(r"is (.+?)([ \.]|$)")
found_patterns = re.findall(p1,text)
[elt[0] for elt in found_patterns]
# ['blue', 'orange']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM