wordlist = ['hypothesis' , 'test' , 'results' , 'total']
的名單上有一句話
sentence = "These tests will benefit in the long run."
我要檢查,看看是否在wordlist
的詞在句子。我知道,你可以檢查,看看他們是否正在使用中的一句話子:
for word in wordlist:
if word in sentence:
print word
但是,使用子,我開始匹配不在wordlist
的話,例如這裏test
將顯示爲一個子即使它是句子中的tests
。我可以通過使用正則表達式來解決我的問題,但是,是否可以通過用每個新單詞格式化的方式實現正則表達式,這意味着如果我想查看該單詞是否在句子中,則:
for some_word_goes_in_here in wordlist:
if re.search('.*(some_word_goes_in_here).*', sentence):
print some_word_goes_in_here
所以在這種情況下,正則表達式會將some_word_goes_in_here
解釋爲需要搜索的模式,而不是some_word_goes_in_here
的值。有沒有一種方法來格式化輸入some_word_goes_in_here
,以便正則表達式搜索some_word_goes_in_here
的值?
如果你有更好的溶膠我渴望聽到它。 – kolonel