Skip to content
Advertisement

Do regular expressions from the re module support word boundaries (b)?

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("btwob", 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?

Advertisement

Answer

You should be using raw strings in your code

>>> x = 'one two three'
>>> y = re.search(r"btwob", x)
>>> y
<_sre.SRE_Match object at 0x100418a58>
>>> 

Also, why don’t you try

word = 'two'
re.compile(r'b%sb' % word, re.I)

Output:

>>> word = 'two'
>>> k = re.compile(r'b%sb' % word, re.I)
>>> x = 'one two three'
>>> y = k.search( x)
>>> y
<_sre.SRE_Match object at 0x100418850>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement