Regular expression to combine numbers

I have a sequence of numbers as shown below. I want to combine the numbers into a group of 4. Can someone give a vim regex for this?

Entrance: 1234 56 7890 1234

The output should be: 1234 5678 9012 34

+3
source share
4 answers

Two passes:

s/ //g
s/\(.\{4\}\)/\1 /g
+10
source

I would do this in two steps: (1) remove spaces to the right of groups of numbers

:s/\(\d\+\) /\1/g

(2) grouping:

:s/\(\d\{4}\)/\1 /g

In the case where many lines record a macro or follow these steps for the marked area.

+4
source

, :

:s/\(\d\) *\(\d\) *\(\d\) *\(\d\) */\1\2\3\4 /g

, asinine. , 2 Monsteregex β„’, . , .

+4
:s/\(\d\)\s*\(\d\)\s*\(\d\)\s*\(\d\)\s*/\1\2\3\4 /g

, .

+1
source

Source: https://habr.com/ru/post/1709082/


All Articles