\b
only matches in between a \w
(word) and a \W
(non-word) character, or vice versa, or when a \w
character is at the edge of a string (start or end).
Your string starts with a /
forward slash, a non word character, so \W
. \b
will never match between the start of a string and /
. Don't use \b
here, use an explicit negative look-behind for a word character :
re.search(r'(?<!\w){}\b'.format(re.escape(l)), s)
The (?<!...)
syntax defines a negative look-behind; like \b
it matches a position in the string. Here it'll only match if the preceding character (if there is any) is not a word character.
I used string formatting instead of concatenation here, and used re.escape()
to make sure that any regular expression meta characters in the string you are searching for are properly escaped.
Demo:
>>> import re
>>> l = "/Acroform "
>>> s = "/Acroform is what I'm looking for"
>>> if re.search(r'(?<!\w){}\b'.format(re.escape(l)), s):
... print 'Found'
...
Found