danilo
danilo

Reputation: 844

Finding a group of words using Regular Expressions

I am using python to get user input and then by using regular expressions I want to check for certain words. In this case I want to check how the user is feeling and then store it in a list. The problem is that when I print the list it is empty.

import re

phrase = raw_input("How are you feeling ")

phrase = phrase.lower()
feel=(re.findall(r^(?=.*\bsad\b)(?=.*\bhappy\b)(?=.*\bjoyful\b)(?=.*\bmad\b)(?=.*\bsad\b), phrase))
print feel

Upvotes: 1

Views: 65

Answers (2)

Chad Grant
Chad Grant

Reputation: 45382

I'm not a python expert, but am fairly decent with regex. Why wouldn't you just use something like:

\b(happy|sad|joyful|mad)\b

Upvotes: 1

walid toumi
walid toumi

Reputation: 2282

Add chars to match

  ...(?=.*\bsad\b).*

Upvotes: 0

Related Questions