Knowledge.ToString()

Simple Trick to Replace Multiple Spaces With Single Tab Character

If you have data with multiple space characters and you wish you could replace all the multiple spaces with tab characters (I bet you want to paste it into Excel), here is a quick trick – Use RegEx in Notepad++.

  • If you don’t have Notepad++, download and install it.
  • Open the Notepad++ and paste the text into new document
  • Go to Search menu > Replace… (Shortcut Ctrl+R)
  • Paste or type "([ ]+)" (without quotes) in “Find what…” box
  • Paste or type "\t" (without quotes) in “Replace with…” box
  • Select “Regular expression” in “Search Mode” section.
    Notepad++ replace multiple spaces with tab character
  • Click “Find Next” button to make sure that it find the right text
  • Click “Replace” to make sure that it replaces it correctly. Use it multiple times before you click “Replace All”.
  • Once done, set the “Search Mode” to “Normal” again

Regex Explanation

Here is what Regex does in layman’s terms.

([ ]+)

Bracket () represents regex group

Square brackets [] represent individual characters within it to search for. Note that there is a space character in between [ and ].

+ represents 1 or more characters.

So it says to find a 1 or sequence of multiple consecutive space characters

\t

\t represents a single tab character

Regex to Replace Exactly 2 Spaces with Tab Character

If you have a need to replace exactly 2 spaces, use following regex for “Find what” box.

([ ][ ])

or

([ ]{2,2})

Regex to Replace Exactly X Spaces with Tab Character

If you have a need to replace exactly 3,4,5,6 and so on spaces, use the following regex for “Find what” box. Replace X from regex with number of space characters you want to replace.

([ ]{X,X})

Regex to Replace X or More Spaces with Tab Character

If you have a need to replace X or more spaces, use the following regex for “Find what” box. Replace X from regex with number of space characters you want to replace.

([ ]{X})

Regex to Replace X or More But Less Than or Equal to Y Spaces with Tab Character

If you have a need to replace spaces which are X or more but less than or equal to Y, use the following regex for “Find what” box. Replace X and Y from regex with minimum and maximum number of spaces you want to replace.

([ ]{X,Y})

Share

Comments

10 responses to “Simple Trick to Replace Multiple Spaces With Single Tab Character”

  1. WD Avatar
    WD

    Looks fine, but wondering why not \s+ instead of [ ]+.

    1. Vishal Monpara Avatar
      Vishal Monpara

      Hi WD,

      \s+ works and handles white space character list that includes special characters in addition to regular space. If someone with less experience in regex (or non technical person) is looking to replace a space character and want to quickly extend the same solution for other characters, they can easily understand the concept and apply the same logic as per their need.

      Regards
      Vishal Monpara

  2. Hasan Avatar
    Hasan

    Hey bro, the “X or More But Less Than or Equal to Y Spaces” saved me a lot of time, thanks for sharing 👍😃

  3. matthieu leroyer Avatar
    matthieu leroyer

    “([ ]+)” -> “\t”
    why creating a class and a group for describing a single space char ?
    ” +” -> “\t”
    does the same

    and about repetitions : {2,2} is the same as {2}
    2 or more is {2,}

    1. Vishal Monpara Avatar
      Vishal Monpara

      Hi Matthieu,

      In this case, class is not needed and what you suggested will work. Thank you for sharing.

      Regards,
      Vishal Monpara

  4. Dilip S. Avatar
    Dilip S.

    Hi Vishal,

    Can we replace multiple tabs with different strings in Notepad++?

    For example: Following is the single text line with tab delimiter

    Test Test Test

    I want to replace first tab with “First” string and Second tab with “Second” string like below.

    Test First Test Second Test

    Thanks.

    1. Vishal Monpara Avatar
      Vishal Monpara

      Hello Dilip,

      If you want to make this change only for one time and not in repeated/automated fashion, instead of using Notepad++, you may use Excel. As you already have data with tab delimiter, copy and paste text into Excel. Then make any changes as per your need by inserting new columns. Once done, copy from Excel and paste into Notepad++. Then replace all tabs with blank.

      If you need to make this change multiple times or want to automate it, you need to create a complex RegEx based on your data.

      Regards,
      Vishal Monpara

  5. Carlo Avatar
    Carlo

    Thank you. It works in VS Code

  6. ganesh Avatar
    ganesh

    Thank you chad.. ([ ]{2,}) worked for me..

  7. Chad Schultz Avatar
    Chad Schultz

    Based on the title of multiple spaces; the regex should be ([ ]{2,})

Leave a Reply

Your email address will not be published. Required fields are marked *