I have a sequence of digits like below. I want to combine digits to group of 4. Can someone give a vim regex to do that?
Input : 1234 56 7890 1234
The output should be: 1234 5678 9012 34
I have a sequence of digits like below. I want to combine digits to group of 4. Can someone give a vim regex to do that?
Input : 1234 56 7890 1234
The output should be: 1234 5678 9012 34
I would do this in two steps: (1) remove the blanks right of digit groups
:s/\(\d\+\) /\1/g
(2) grouping:
:s/\(\d\{4}\)/\1 /g
In case of many lines record a macro
or do these steps for an marked area.
You can do it in one pass, but it looks awful:
:s/\(\d\) *\(\d\) *\(\d\) *\(\d\) */\1\2\3\4 /g
While this works, it's rather asinine. It's faster to do one of those nice two-pass solutions than to spend 2 minutes working out one Monsteregex™ that does it in one pass. Plus, the two-pass solutions are easier to understand.
:s/\(\d\)\s*\(\d\)\s*\(\d\)\s*\(\d\)\s*/\1\2\3\4 /g
works, but I prefers Autocracy's solution.