Help with Regexp, \b

Shashwat Anand anand.shashwat at gmail.com
Sat May 29 11:28:28 EDT 2010


Also what you are probably looking for is this I guess,

>>> p = re.compile(r'\bword\b')
>>> m = p.match('word word')
>>> assert m
>>> m.group(0)
'word'


On Sat, May 29, 2010 at 8:44 PM, Shashwat Anand <anand.shashwat at gmail.com>wrote:

> \b is NOT spaces
>
> >>> p = re.compile(r'\sword\s')
> >>> m = p.match(' word ')
> >>> assert m
> >>> m.group(0)
> ' word '
> >>>
>
>
> On Sat, May 29, 2010 at 8:34 PM, andrew cooke <andrew at acooke.org> wrote:
>
>>
>> This is a bit embarassing, but I seem to be misunderstanding how \b
>> works in regexps.
>>
>> 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.
>>
>> Thanks,
>> Andrew
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100529/739851ce/attachment.html>


More information about the Python-list mailing list