Do regular expressions from the re module support word boundaries (\b)?
Just to explicitly explain why re.search("\btwo\b", x)
doesn't work, it's because \b
in a Python string is shorthand for a backspace character.
print("foo\bbar")
fobar
So the pattern "\btwo\b"
is looking for a backspace, followed by two
, followed by another backspace, which the string you're searching in (x = 'one two three'
) doesn't have.
To allow re.search
(or compile
) to interpret the sequence \b
as a word boundary, either escape the backslashes ("\\btwo\\b"
) or use a raw string to create your pattern (r"\btwo\b"
).
This will work: re.search(r"\btwo\b", x)
When you write "\b"
in Python, it is a single character: "\x08"
. Either escape the backslash like this:
"\\b"
or write a raw string like this:
r"\b"
You should be using raw strings in your code
>>> x = 'one two three'
>>> y = re.search(r"\btwo\b", x)
>>> y
<_sre.SRE_Match object at 0x100418a58>
>>>
Also, why don't you try
word = 'two'
re.compile(r'\b%s\b' % word, re.I)
Output:
>>> word = 'two'
>>> k = re.compile(r'\b%s\b' % word, re.I)
>>> x = 'one two three'
>>> y = k.search( x)
>>> y
<_sre.SRE_Match object at 0x100418850>