Replace any number of spaces using regular expressions

54,290

Solution 1

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)

Solution 2

Taken from here:

Use as "find" expression:

 {1,}

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.

Share:
54,290

Related videos on Youtube

Antonio
Author by

Antonio

Updated on September 18, 2022

Comments

  • Antonio
    Antonio over 1 year

    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.

    • hBy2Py
      hBy2Py about 9 years
      Does "any number of consecutive whitespaces" mean one or more, or two or more?
    • Antonio
      Antonio about 9 years
      @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
      Antonio about 9 years
      What the heck, why was the question downvoted?!?
    • hBy2Py
      hBy2Py about 9 years
      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.
    • Antonio
      Antonio about 9 years
      @Brian Yep, but I don't think a question should be downvoted, supposedly, for lack of research when also an answer is provided!!!... Thanks for the feedback!
    • Antonio
      Antonio about 9 years
      @Brian There it is!
    • Antonio
      Antonio about 9 years
      @Brian For completeness, that's the proposal at meta.stackexchange.com/questions/252190/… (It was made independent from this question, on which there were different opinions). It was not very successful, but at least I was not the only one who thought the feature could be useful :)
  • barlop
    barlop about 9 years
    @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
    barlop about 9 years
    @DavidPostill but Antonio's link was a horrible reference, because Word is different and can have its own syntax.
  • Antonio
    Antonio about 9 years
    @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
    Antonio about 9 years
    @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.
  • hBy2Py
    hBy2Py about 9 years
    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.
  • barlop
    barlop about 9 years
    There is also a bit of a mixup between spaces and whitespaces in the question..where the title says space and the first line says whitespace. I think you meant whitespaces..
  • Ramhound
    Ramhound about 9 years
    I issued a downvote because you should have pulled more content from the referenced website. I believe this answer to be incomplete if one were NOT to visit that website.
  • Antonio
    Antonio about 9 years
    @Ramhound Are you speaking about the current (at the time of downvote) version or about the original version of the answer? I believe as it is now it takes all necessary parts (indeed, very little), now the website is mainly linked to acknowledge authorship.
  • Ramhound
    Ramhound about 9 years
    @Antonio - I am judging the usefulness of the answer based on the current revision.
  • Antonio
    Antonio about 9 years
    @Ramhound I struggle to understand how, removed the first line (authorship acknowledgment), the answer can be considered not sufficiently documented.
  • Ramhound
    Ramhound about 9 years
    @Antonio - I have express the reason for the downvote. When my concern is address I will reverse it. But I won't be spending more time explaining the reason.
  • Antonio
    Antonio about 9 years
    @Ramhound You said that the answer is missing fundamental information contained in the link. I am trying to understand what is the missing information you are referring to (which you did not specify) and possibly improve the answer, not to waste your time.
  • Ramhound
    Ramhound about 9 years
    @Antonio - Honestly. I only made that comment since I feel you will just argue about anything I actually point out.
  • Antonio
    Antonio about 9 years
    @Ramhound It seems we're on the same ship (a pretty crowded one :) )