Simple (?) Regular Expression Question
tim.one at comcast.net
Mon Jan 19 01:01:00 CET 2004
> Is '@' a special character in regular expressions?
> I am asking because I don't understand the following:
> >>> import re
> >>> s = ' @'
> >>> re.sub(r'\b@','*',s)
> ' @'
> >>> s = ' a'
> >>> re.sub(r'\ba','*',s)
> ' *'
\b matches a "word boundary", meaning it has to have a word character on one
side (something that matches \w), and a non-word character on the other
(something that matches \W), regardless of order. ' @' contains two
non-word characters (' ' and '@'), so \b doesn't match anything in it. ' a'
contains a non-word character (' ') followed by a word character ('a'), so
\b matches (an empty string) betwen those two characters.
More information about the Python-list