Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using regex in notepad++ to add new line every nth occurrence of a space

Tags:

regex

This must have been asked before but can't find the answer I need.

I have a very large file with a long string of integers separated by spaces. The intergers are 3 or 4 characters long.

I want to separate the rows so that I end up with 5 columns of numbers.

I am trying to do this with notepad++ as its the only tool at my disposal at the moment.

Example of what I have

123 123 123 123 123 123 123 123 123 123 456 456 456 456 456 456 1231 2312 3112 1534 1254 1254 1254 2154 1522 1233 1235 1231 1234
1123 4564 1534 1321 1234 1234 1234 1234 1234 123 123 123 123 123 123 132 123 123
1234 1234 1234 1234 4567 4567 4567 4567 4567 145 154 154 154

What I need is

123 123 123 123 123
123 123 123 123 123
456 456 456 456 456
456 1231 2312 3112 1534
1254 1254 1254 2154 1522
1233 1235 1231 1234 1123
4564 1534 1321 1234 1234
1234 1234 1234 123 123
123 123 123 123 132
123 123 1234 1234 1234
1234 4567 4567 4567 4567
4567 145 154 154 154

Things are complicated by the fact that the rows are not equal length.

Hope someone can help.

PS: would be good if the final example had the spaces replaced with tabs also!

like image 950
Alex Ball Avatar asked Jul 27 '15 03:07

Alex Ball


2 Answers

All these replacements are done with regular expressions and ". matches newline":

  1. First replace \s+ with a space
  2. Replace ((\d+\s+){5}) with \1\n
  3. (optional) Replace spaces with \t (can also do it at the beginning)

Note that it doesn't look good with the default tab size of 4 however. You need at least 5 for your example.

like image 83
Blindy Avatar answered Oct 07 '22 22:10

Blindy


Find what: (\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+

Replace with: \1 \2 \3 \4 \5\n

(respectively \1\t\2\t\3\t\4\t\5\n to replace spaces with tabs)

Search Mode: Regular expression

like image 34
Carsten Hagemann Avatar answered Oct 08 '22 00:10

Carsten Hagemann