1

文に「go * to」というフレーズが含まれているかどうかを確認しようとしています。たとえば、「go over to」、「go up to」などです。Textblob を使用していますが、以下で使用できることがわかっています。

search_go_to = set(["go", "to"])
go_to_blob = TextBlob(var)
matches = [str(s) for s in go_to_blob.sentences if search_go_to & set(s.words)]
print(matches)

しかし、それは「そこに行き、これを彼に持ってきてください」のような文も返します。これは望ましくありません。text.find("go * to") のようなことができる方法を知っている人はいますか?

4

4 に答える 4

3

使用してみてください:

for match in re.finditer(r"go\s+\w+\s+to", text, re.IGNORECASE):
于 2015-01-17T22:05:01.863 に答える
1

これを試して

text = "something go over to something"

if re.search("go\s+?\S+?\s+?to",text):
    print "found"
else:
    print "not found"

正規表現:-

\s is for any space
\S is for any non space including special characters
+? is for no greedy approach (not required in OP's question)

re.search("go\s+?\S+?\s+?to",text)一致するので"something go W#$%^^$ to something"、もちろんこれも"something go over to something"

于 2015-01-17T22:36:25.093 に答える
0

これは機能しますか?

import re
search_go_to = re.compile("^go.*to$")
go_to_blob = TextBlob(var)
matches = [str(s) for s in go_to_blob.sentences if search_go_to.match(str(s))]
print(matches)

正規表現の説明:

^    beginning of line/string
go   literal matching of "go"
.*   zero or more characters of any kind
to   literal matching of "to"
$    end of line/string

「going to」を一致させたくない場合は、 の前後に (単語境界) を挿入し\\bます。togo

于 2015-01-17T21:59:24.160 に答える