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

This must have been asked before, but cannot find the answer I want.

I have a very large file with a long string of space-separated integers. Interiors are 3 or 4 characters long.

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

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

An 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

      

I need

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 strings are not equal.

Hope someone can help.

PS: it would be nice if in the last example the spaces were also replaced by tabs!

+3


source to share


2 answers


All of these replacements are done using regular expressions and ". Matches newline":

  • First replace with a \s+

    space
  • Replace ((\d+\s+){5})

    with\1\n

  • (optional) Replace spaces with \t

    (can also do this at the beginning)


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

+5


source


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

Replaced by: \1 \2 \3 \4 \5\n



(appropriately \1\t\2\t\3\t\4\t\5\n

to replace tabbed spaces)

Search Mode: Regular expression

0


source







All Articles