[英]how to increment a whole word match in python using a variable in the regex
我试图显示使用python和regex在txt文件中匹配多少单词,但不使用术语'like'我想使用变量'words'
text = 'I like and love green grass'
positive_words = positive_words=open("positive.txt").read()
words = text.split(' ')
if re.search(r'\blike\b',positive_words):
positive_counter=positive_counter+1
print positive_counter
在我的txt文件中,我有'喜欢'和'爱'的字样,所以positive_counter应该等于2 ..我如何使用单词作为变量而不是'喜欢'? 这现在有效但只是不知道如何合并变量词
text = 'I like and love green grass'
positive_words = positive_words=open("positive.txt").read()
words = text.split(' ')
for word in words:
if re.search(r'\b' + word + r'\b',positive_words):
positive_counter=positive_counter+1
print positive_counter
只需循环文本中的所有单词。
从正则表达式的角度来看,这应该工作:
re.search(r'\b(I|like|and|love|green|grass)\b', positive_words)
要从你的文本变量构建re(注意,我是从内存编写的,你可能需要稍微调整一下):
regex = r'\b(%s)\b' % "|".join(words)
re.search(regex, positive_words)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.