Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching whole words using "in" in python

Tags:

python

I've been searching around for some time for this, but have still not found an answer, maybe its got some thing to do with regular expressions, but i think there should be a simple answer that I am missing here. It seems very trivial to me ... here goes:

On the python interpreter I get:

"abc" in "abc123" 

as True.

I want it a command that returns a False. I want the entire word to be matched.

Thanks!

like image 234
Rahul Avatar asked Feb 28 '12 05:02

Rahul


1 Answers

in isn't how it's done.

>>> re.search(r'\babc\b', 'abc123')
>>> re.search(r'\babc\b', 'abc 123')
<_sre.SRE_Match object at 0x1146780>
like image 136
Ignacio Vazquez-Abrams Avatar answered Sep 28 '22 02:09

Ignacio Vazquez-Abrams