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++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -