[英]Python searching for two words regex
我試圖找出一個句子中是否包含短語“ go * to”,例如“ go to 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”)?
嘗試使用:
for match in re.finditer(r"go\s+\w+\s+to", text, re.IGNORECASE):
使用generator expressions
>>> search_go_to = set(["go", "to"])
>>> m = ' .*? '.join(x for x in search_go_to)
>>> words = set(["go over to", "go up to", "foo bar"])
>>> matches = [s for s in words if re.search(m, s)]
>>> print(matches)
['go over to', 'go up to']
嘗試這個
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"
這樣行嗎?
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
如果你不想“要”來匹配,插入\\\\b
之前(字邊界) to
后go
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.