Simple (?) Regular Expression Question
Tim Peters
tim.one at comcast.net
Sun Jan 18 19:01:00 EST 2004
[Steve Zatz]
> Is '@' a special character in regular expressions?
Nope.
> 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
mailing list