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