4
我有一些文本是句子,其中一些是問題。我試圖創建一個正則表達式將僅提取包含特定的詞,即「國家科學基金會」的問題:如何使用python re在文本中找到包含短語的句子?
import re
s = "This is a string. Is this a question? This isn't a question about NSF. Is this one about NSF? This one is a question about NSF but is it longer?"
理想情況下,re.findall將返回:
['Is this one about NSF?','This one is a question about NSF but is it longer?']
但我目前最好的嘗試是:
re.findall('([\.\?].*?NSF.*\?)+?',s)
[". Is this a question? This isn't a question about NSF. Is this one about NSF? This one is a question about NSF but is it longer?"]
我知道我需要做的事情與非貪婪的煩躁,但我不知道在那裏我搞亂了。
嘗試'R'\ S *(?[?] [?^] * NSF [?^] *) '' –
@WiktorStribiżew謝謝!你能解釋一下你爲了幫助自己理解而做出的改變嗎? – zephyr1999
我正在給孩子們上牀睡覺。那麼,它對你有用嗎?關鍵是我使用否定字符類來匹配除特定字符以外的文本塊。 –