4
私は文章であるテキストをいくつか持っていますが、そのうちのいくつかは質問です。私は特定のフレーズ、つまり「NSF」を含む質問のみを抽出します正規表現を作成しようとしています:理想的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?"]
私は非貪欲ネスで何かをする必要がありますが、私はめちゃくちゃよどこか分からない知っています。
\([^。?] * NSF [^。?] *??[?])S * 'r 'を試してみてください'' –
@WiktorStribiżewありがとう!自分の理解を助けるためにあなたが少しした変更を説明できますか? – zephyr1999
私は子供たちを寝たきりにしていました。それで、それはあなたのために働くのですか?要点は、ネガティブ文字クラスを使用して、特定の文字以外のテキストチャンクにマッチさせることです。 –