Ryan D
Ryan D

Reputation: 751

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'

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'? This works now but just do not know how to incorporate the variable words

Upvotes: 3

Views: 919

Answers (2)

James
James

Reputation: 2711

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.

Upvotes: 4

Roadmaster
Roadmaster

Reputation: 5357

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):

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

Upvotes: 1

Related Questions