How can I replace any number of consecutive spaces, with, for example, a tab in a text editor supporting regular expressions (like Notepad++)

To clarify: Replacing every occurrence of one or more spaces, with (for example) a tab. All spaces will be gone after substitution.

  • Does "any number of consecutive whitespaces" mean one or more, or two or more?
    – hBy2Py
    Mar 25, 2015 at 18:14
  • @Brian I meant one or more, I had not read carefully the link I posted in my answer, which is clearly mentioning multiple spaces. I have edited my answer.
    – Antonio
    Mar 25, 2015 at 23:34
  • What the heck, why was the question downvoted?!?
    – Antonio
    Mar 25, 2015 at 23:51
  • 1
    Someone probably thought it was too trivial. The syntax you need for this is one of the most basic elements of Regex. Still, as long as the question isn't duplicative, there may be value in having it as a part of SE.
    – hBy2Py
    Mar 25, 2015 at 23:56
  • 1
    @Brian There it is!
    – Antonio
    Mar 26, 2015 at 0:25

2 Answers 2


How do I replace any number of spaces using regular expressions

Notepad++ Solution

To match one or more space characters:

  • Set "Find what" to + (space followed by +)

To match one of more whitespace characters (space, EOL, and tab all count as whitespace):

  • Set "Find what" to \s+

    Warning: Using \s+ will match end of line and therefore join multiple lines together (separated by the "replace with" string)

To replace with a tab character:

  • Set "Replace with" to \t

To enable regular expression (so the above special codes will work)

  • Select "Regular expression".

enter image description here

Source How to use regular expressions in Notepad++ (tutorial)


Taken from here:

Use as "find" expression:


namely a space followed by {1,}.

To replace with tab, enter ^t in the replace box. Don't forget to activate regular expressions.

This link covers the syntax of the given regex. Below is an extract of a relevant part.

{n,} Matches when the preceding character occurs at least n times, for example, ba{2,}b will find 'baab', 'baaab' or 'baaaab' but NOT 'bab'. Values are enclosed in braces (curly brackets).

For the records, it has been tested on notepad++ (See here, courtesy of barlop). You can also put a \t in the replace box.

  • 2
    @DavidPostill It works for Notepad++ it's just a regular expression. Not ms word specific. see here i.stack.imgur.com/z7OjC.png And doing {2,} is better than your ` +` because your one as you know will also replace a single/individual space. And this solution has the flexibility that the number can be changed. Whereas + is only for "one or more", and it's likely the OP meant 2 or more.
    – barlop
    Mar 25, 2015 at 19:31
  • @DavidPostill but Antonio's link was a horrible reference, because Word is different and can have its own syntax.
    – barlop
    Mar 25, 2015 at 19:37
  • @barlop The link was the first result from google. And worked. I thought it could be useful to have the question answered also on the Stack website. I meant 1 or more :) and I was surprised in seeing the 2 there, worked in my use case because at each occurrence I had at least 2 spaces. Now the regular expression is much clearer, thanks!
    – Antonio
    Mar 25, 2015 at 23:33
  • @barlop Again, about the link, I think it's not a bad guide at all, since ,despite being a Word environment, it uses regular expressions which are very general. It's just lacking some explanations of the mechanism (as you duly provided, thanks) which, however, it's probably out of reach for the target of that article.
    – Antonio
    Mar 25, 2015 at 23:47
  • Regex is not truly universal. Different implementations of Regex support a subset of all the various functionalities that are out there, and the syntax for them can differ subtly or dramatically between implementations. In my own experience, for example, the Regex syntax in MS Office VBA and in Python are pretty dramatically different.
    – hBy2Py
    Mar 25, 2015 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.