Regex to select only specific number of spaces

notepadregex

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

Best Answer

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

 ^(space_character){number_of_spaces_to_be_found}
.
For example, the following regex will find 4 spaces
 ^ {4}
.
In the replace box use
"\t"
for replacing your find with one tab.
For two tabs use
"\t\t"
.

Related Question