2

Why does the following Python statement return None?

>>> re.match('\b\w+\b', 'foo')
>>>

As far as I understand, that should match the word foo. The first \b should match the beginning of the word foo, \w+ should match the word foo, and the final \b should match the end of the word foo. What is wrong in my understanding?

Lone Learner
  • 18,088
  • 20
  • 102
  • 200

1 Answers1

8

If you don't escape backslash in \b, \b matches backspace, not word boundary.

>>> '\b'    # BACKSPACE, not \ + b
'\x08'
>>> '\\b'   # \ + b
'\\b'
>>> r'\b'   # raw string literal (r'\b' == '\\b')
'\\b'

>>> re.match('\b\w+\b', 'foo')
>>> re.match(r'\b\w+\b', 'foo')
<_sre.SRE_Match object at 0x0000000002C18100>
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • more on [Regular expression HOWTO / More-metacharacters](https://docs.python.org/2/howto/regex.html#more-metacharacters) – Cavaz Jun 18 '14 at 08:55