I am using notepad++ and I want to do a find & replace operation.
For example

  • 4 number of spaces means 1 tab
  • 6 number of spaces means 2 tabs etc.

In my file all the spaces are in the beginning of each line.

What is the regex I should use to find the exact number of spaces?
I want to replace the spaces with tabs (like single or double tabs based on 4 spaces or 6 spaces)

Note: The file is a classification file which explains that 4 spaces is parent and 6 spaces is child and 8 spaces is a child of a child.

a Sample of the file :

Agriculture, forestry and fishing
 Crop and animal production, hunting and related service activities
   Growing of non perennial crops
     Growing of cereals (except rice), leguminous crops and oil seeds
     Growing of rice
     Growing of vegetables and melons, roots and tubers
  • 1
    4 spacs = 1 tab - 5 spaces = 2 tabs - 7 spaces = 3 tabs - 9 spaces = 4 tabs – Ahmed AbuRa'ed Mar 29 '15 at 10:42

The syntax for regex to find out the number of spaces in the beginning of a line is

For example, the following regex will find 4 spaces
 ^ {4}
In the replace box use
for replacing your find with one tab.
For two tabs use

| improve this answer | |
  • Yes but this regexp will also get the lines that has 6 spaces or any number higher than 4 i want it to check if the line have exactly 4 spaces followed by the rest of the line (any character) – Ahmed AbuRa'ed Mar 29 '15 at 10:23
  • Agreed. You can use ^ {6} and replace them with double tabs and then do the ^ {4} and replace them with single tabs - I agree that it is not the best solution - i'm still learning REGEX – Prasanna Mar 29 '15 at 10:36
  • Yap i think that will solve the problem. Thanks – Ahmed AbuRa'ed Mar 29 '15 at 10:41
  • Try adding the dollar symbol in the expression ^ {4} - so that it becomes like this ^ {4}$. If this solves your question - please accept my answer – Prasanna Mar 29 '15 at 10:49
  • it is ok i did a work around and manage to solve it by starting from the last classifications going into the parents. Finally i had to do the parents manually because they will be detected in a wrong way but luckily they are not much since they are parents so i did them manually. – Ahmed AbuRa'ed Mar 29 '15 at 12:45

Perhaps the following pattern will do what you want (angle brackets surround the patterns):

Four leading spaces become one tab

Find: <^ {4}([^ ].*)$>
Replace: <\t\1>

Six leading spaces become two tabs

Find: <^ {6}([^ ].*)$>
Replace <\t\t\1>


  • As noted in other answers, ^ matches the start of a line.
  • Also as noted, the curly bracket notation {#} specifies a match for a specific number of repetitions.
  • The following content in square brackets, [^ ], is a character class matching a single character that is not a space (square brackets define the character class; the leading ^ here indicates to invert the class).
  • .*$ matches any number of any kind of characters (possibly including newlines, depending on how the Regex engine is set up) up to the end of the line.
  • The parentheses surrounding ([^ ].*) indicate a defined group within the pattern, which is (hopefully, depending on the Regex engine in Notepad++) retrieved in the Replace expression by \1.
| improve this answer | |
  • doesn't work on my place. Can you make an example on regex101.com ? – Just Me Jan 28 at 8:04

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.