\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
None
>>> 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.

Answer from Rob Watts on Stack Overflow
Top answer
1 of 4
36

\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
None
>>> 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.

2 of 4
7

\s+ is not equivalent to \t because \s does not mean <space>, but instead means <whitespace>. A literal space (sometimes four of which are used for tabs, depending on the application used to display them) is simply . That is, hitting the spacebar creates a literal space. That's hardly surprising.

\s\s will never match a \t because since \t IS whitespace, \s matches it. It will match \t\t, but that's because there's two characters of whitespace (both tab characters). When your regex runs \s\s+, it's looking for one character of whitespace followed by one, two, three, or really ANY number more. When it reads your regex it does this:

\s\s+

Debuggex Demo

The \t matches the first \s, but when it hits the second one your regex spits it back out saying "Oh, nope nevermind."

Your first regex does this:

\s\s*

Debuggex Demo

Again, the \t matches your first \s, and when the regex continues it sees that it doesn't match the second \s so it takes the "high road" instead and jumps over it. That's why \s\s* matches, because the * quantifier includes "or zero." while the + quantifier does not.

🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations
May 25, 2026 - Source code: Lib/re/ This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings ( str) as well as 8-...
Discussions

Regex pattern that excludes whitespaces
Hello I’m using this regex pattern to extract this text succesfully: Regex Pattern: tailored_bullet_point_1(.+?)explanation_1 Text: tailored_bullet_point_1": "Collaborated seamlessly across teams, ensuring detailed integration of academic and social-emotional aspects into comprehensive learning ... More on forum.bubble.io
🌐 forum.bubble.io
8
0
April 13, 2024
RegEx for Horizontal Whitespace (\s \h \t blank etc)
This is probably the most often recommended, but it is too aggressive for my tastes: \s Most engines: "whitespace character": space, tab, newline, carriage return, vertical tab So I have gotten into the habit of just replacing TABs and SPACEs: [ \t]+ It just depends on your needs, and the source ... More on forum.keyboardmaestro.com
🌐 forum.keyboardmaestro.com
1
1
September 26, 2017
Inserting a whitespace before some characters (with regex?)
You can put the characters into a set, create a group, and add the whitespace to any group match: import re s = 'This is a such a simple example,... but very useful.' print(re.sub(r'([,!?.]+)', r' \1', s)) # 'This is a such a simple example ,... but very useful .' More on reddit.com
🌐 r/learnpython
6
3
November 24, 2018
Python regex: Including whitespace inside character range - Stack Overflow
I have a regular expression that matches alphabets, numbers, _ and - (with a minimum and maximum length). ^[a-zA-Z0-9_-]{3,100}$ I want to include whitespace in that set of characters. According ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Basic Python
astuntechnology.github.io › python-basics › regular-expressions.html
Python Regular Expressions | Basic Python
\s – (lowercase s) matches a single whitespace character – space, newline, return, tab, form [ \n\r\t\f]. \S (upper case S) matches any non-whitespace character. ... \d – decimal digit [0-9] (some older regex utilities do not support but \d, but they all support \w and \s)
🌐
RegexOne
regexone.com › lesson › whitespaces
RegexOne - Learn Regular Expressions - Lesson 9: All this whitespace
The most common forms of whitespace you will use with regular expressions are the space (␣), the tab (\t), the new line (\n) and the carriage return (\r) (useful in Windows environments), and these special characters match each of their respective whitespaces.
🌐
Bubble
forum.bubble.io › questions
Regex pattern that excludes whitespaces - Questions - Bubble Forum
April 13, 2024 - Hello I’m using this regex pattern to extract this text succesfully: Regex Pattern: tailored_bullet_point_1(.+?)explanation_1 Text: tailored_bullet_point_1": "Collaborated seamlessly across teams, ensuring detailed integration of academic and social-emotional aspects into comprehensive learning plans, showcasing @strong communication@ and commitment to personalized experiences for stakeholders.",\n "explanation_1\ There are 4 white spaces after the \n at the end of the text right before "...
🌐
W3Schools
w3schools.com › python › python_regex.asp
Python RegEx
RegEx can be used to check if a string contains the specified search pattern. Python has a built-in package called re, which can be used to work with Regular Expressions.
Find elsewhere
🌐
4Geeks
4geeks.com › en › how-to › regex-for-whitespace
Regex for Whitespace: Every Pattern Explained (2026)
July 16, 2025 - \s+ When added the +, you are stating that can be one, or any amount of whitespaces (spaces, tabs, and line breaks). [\r\n\t\f\v ] This RegEx is equivalent to \s but we add it for ompatibility reasons with older programming languages.
🌐
Google
developers.google.com › google for education › python › python regular expressions
Python Regular Expressions | Python Education | Google for Developers
\s -- (lowercase s) matches a single whitespace character -- space, newline, return, tab, form [ \n\r\t\f]. \S (upper case S) matches any non-whitespace character. ... \d -- decimal digit [0-9] (some older regex utilities do not support \d, ...
🌐
Medium
geekpython.medium.com › ways-to-remove-whitespaces-from-the-string-in-python-f1e1481a9c33
Ways To Remove Whitespaces From The String In Python | by Sachin Pal | Medium
July 29, 2022 - Removed whitespace from the start: G e e k P y t h o n. Here, we used "^\s+" regex pattern to remove whitespace from the beginning of the string.
🌐
Python documentation
docs.python.org › 3 › howto › regex.html
Regular expression HOWTO — Python 3.14.6 documentation
Some of the special sequences beginning with '\' represent predefined sets of characters that are often useful, such as the set of digits, the set of letters, or the set of anything that isn’t whitespace. Let’s take an example: \w matches any alphanumeric character. If the regex pattern ...
🌐
Reddit
reddit.com › r/learnpython › inserting a whitespace before some characters (with regex?)
r/learnpython on Reddit: Inserting a whitespace before some characters (with regex?)
November 24, 2018 -

I have a bunch of strings which I want to preprocess. I want to make my life easier for the following steps by inserting a whitespace before characters like [ , (comma) ! (exclamation mark) ? (question mark) . (period) ]

Lets say I have the following string:

'This is a such a simple example,... but very usefull.'

What I want to get as an output is:

`This is such a simple example ,... but very usefull .' (before the comma and the period is a whitespace)

/EDITED:

If these characters appear in a group, I want to put a whitespace before the group. (See updated example above)

🌐
Scaler
scaler.com › home › topics › how to remove whitespace from string in python?
How to Remove Whitespace From String in Python?
February 14, 2024 - If a character is not a whitespace, concatenate it to the result string; otherwise, skip it. Below is a Python function that demonstrates this method: ... To strip spaces from a string in Python, combining Regular Expressions (Regex) with itertools.filterfalse() offers a concise and effective solution.
🌐
Wikipedia
en.wikipedia.org › wiki › Regular_expression
Regular expression - Wikipedia
June 26, 2026 - The usual context of wildcard characters is in globbing similar names in a list of files, whereas regexes are usually employed in applications that pattern-match text strings in general. For example, the regex ^[ \t]+|[ \t]+$ matches excess whitespace at the beginning or end of a line.
🌐
Regex Generator
regex-generator.olafneumann.org
Regex Generator - Creating regex is easy again!
A tool to generate simple regular expressions from sample text. Enable less experienced developers to create regex smoothly.
🌐
UiPath Community
forum.uipath.com › help › activities
How to ignore whitespace in Regex Expression - Activities - UiPath Community Forum
September 13, 2021 - I have this pdf file that I get my text, using read pdf txt. In that string, I am trying to get this particular line I managed to get the “BUY USD” by using buy [A-Z]{3}. But I also need to get the number on the right side but it has too many spaces and the regex builder won’t even recognize it, even if I put a Digit(\d)
🌐
Real Python
realpython.com › regex-python
Regular Expressions: Regexes in Python (Part 1) – Real Python
October 21, 2023 - In this case, [\d\w\s] matches any digit, word, or whitespace character. And since \w includes \d, the same character class could also be expressed slightly shorter as [\w\s]. ... Occasionally, you’ll want to include a metacharacter in your regex, except you won’t want it to carry its special meaning.