python - Finding words in phrases using regular expression -


i wanna use regular expression find phrases contains 1 - 1 of n words (any) 2 - n words (all )

>>> import re >>> reg = re.compile(r'.country.|.place') >>> phrases = ["this place", "france european country, , wonderful place visit", "paris place, s capital of country.side"]  >>> phrase in phrases: ...     found = re.findall(reg,phrase) ...     print found ...  

result:

[' place'] [' country,', ' place'] [' place', ' country.'] 

it seems messing around, need specify need find word, not part of word in both cases.

can pointing issue ?

because trying match entire words, use \b match word boundaries:

reg = re.compile(r'\bcountry\b|\bplace\b') 

Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -