简体   繁体   English

如何使用正则表达式中的变量在python中增加整个单词匹配

[英]how to increment a whole word match in python using a variable in the regex

I am trying to show how many words match in a txt file using python and regex but instead of using the term 'like' I would like to use the variable 'words' 我试图显示使用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

in my txt file I have the words 'like' and 'love' so positive_counter should equal 2.. How would I use words as a variable instead of 'like'? 在我的txt文件中,我有'喜欢'和'爱'的字样,所以positive_counter应该等于2 ..我如何使用单词作为变量而不是'喜欢'? This works now but just do not know how to incorporate the variable words 这现在有效但只是不知道如何合并变量词

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

Just looping all of the words in text. 只需循环文本中的所有单词。

From the regex point of view, this should work: 从正则表达式的角度来看,这应该工作:

re.search(r'\b(I|like|and|love|green|grass)\b', positive_words)

To build the re from your text variable (note, I'm coding this from memory, you may need to tweak it somewhat): 要从你的文本变量构建re(注意,我是从内存编写的,你可能需要稍微调整一下):

regex = r'\b(%s)\b' % "|".join(words)
re.search(regex, positive_words)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM