Why is it different about '\s' Matches whitespace and Equivalent to [\t\n\r\f]?

MRAB python at mrabarnett.plus.com
Thu Jul 10 13:18:01 CEST 2014


On 2014-07-10 11:05, rxjwg98 at gmail.com wrote:
> Hi,
>
> On a tutorial it says that '\s': Matches whitespace. Equivalent to [\t\n\r\f].
>
It's equivalent to [ \t\n\r\f], i.e. it also includes a space, so
either the tutorial is wrong, or you didn't look closely enough. :-)

> I test it with:
>
>>>> re.match(r'\s*\d\d*$', '   111')
> <_sre.SRE_Match object at 0x03642BB8>
>>>> re.match(r'\t\n\r\f*\d\d*$', '   111')    # fails

The string starts with ' ', not '\t'.

>>>> re.match(r'[\t\n\r\f]*\d\d*$', '   111') # fails
>>>> re.match(r'[\t\n\r\f]\d\d*$', '   111') # fails
>>>> re.match(r'[\t\n\r\f]*$', '   111') # fails

The string starts with ' ', which isn't in the character set.

>
> What is wrong in above script? Thanks
>




More information about the Python-list mailing list