Help with Regexp, \b

Duncan Booth duncan.booth at invalid.invalid
Sat May 29 11:24:48 EDT 2010


andrew cooke <andrew at acooke.org> wrote:

> Please can someone explain why the following fails:
> 
>         from re import compile
> 
>         p = compile(r'\bword\b')
>         m = p.match(' word ')
>         assert m
> 
> My understanding is that \b matches a space at the start or end of a
> word, and that "word" is a word - http://docs.python.org/library/re.html
> 
> What am I missing here?  I suspect I am doing something very stupid.
> 

You misunderstand what \b does: it doesn't match a space, it matches a 0 
length string on a boundary between a non-word and a word.

Try:

 p.match(' word ', 1).group(0)

and you'll see that you are only match the word, not the surrounding 
puctuation.



More information about the Python-list mailing list