Search code examples
regexnotepad++

How to remove the leading space and replace white spaces by tab in one go in Notepad++?


Try to manipulate the text file containing

 -1.     1      1  4.34E+4
 -1.     2      1  4.73E+3
  4.     1      1  4.78E+3  2.8E+3
  4.     7      1  2.62E+3  1.4E+2
  4.     8      1  2.22E+6  7.6E+03
  ...

to be

-1. 1   1   4.34E+4
-1. 2   1   4.73E+3
4.  1   1   4.78E+3 2.8E+3
4.  7   1   2.62E+3 1.4E+2
4.  8   1   2.22E+6 7.6E+03
...

The following two-steps can reach the goal, but is it possible to combine them into one?

Find What:^ + Replace:

followed by

Find What: + Replace:\t


Solution

  • You can use

    Find What: ^(\h+)|\h+
    Replace With: (?{1}:\t) (or (?1:\t) since there is no digit that is a part of a replacement pattern after the ?1 here)

    The ^(\h+)|\h+ pattern captures one or more horizontal whitespace at the start of any line into Group 1 and then matches any one or more horizontal whitespace chars anywhere on a line. Then, the replacement is a TAB char if Group 1 did not match, else, it is an empty string.

    See the screenshot and demo:

    enter image description here