whitespace in regular expression

\t is not equivalent to \s+, but \s+ should match a tab (\t).

The problem in your example is that the second pattern \s\s+ is looking for two or more whitespace characters, and \t is only one whitespace character.

Here are some examples that should help you understand:

>>> result = re.match(r'\s\s+', '\t')
>>> print result
>>> result = re.match(r'\s\s+', '\t\t')
>>> print result
<_sre.SRE_Match object at 0x10ff228b8>

\s\s+ would also match ' \t''\n\t'' \n \t \t\n'.

Also, \s\s* is equivalent to \s+. Both will match one or more whitespace characters.

Leave a Comment