While trying to learn a little more about regular expressions, a tutorial suggested that you can use the \b
to match a word boundary. However, the following snippet in the Python interpreter does not work as expected:
>>> x = 'one two three'
>>> y = re.search("\btwo\b", x)
It should have been a match object if anything was matched, but it is None
.
Is the \b
expression not supported in Python or am I using it wrong?
re.search(r"\btwo\b", x)
– Bolo Oct 22 '10 at 8:39r"\btwo\b"
? – S.Lott Oct 22 '10 at 10:56\b
. – tchrist Nov 18 '10 at 13:55r'\b'
so the character is escaped. (or else double-escape it\\b
, which is yukky) – smci Jun 9 '20 at 9:35