Reputation: 6441
While reading the book "Mastering regular expression", I tried to solve the problem of "commafication" of a number using the vim editor.
How is it possible to turn a number in a file from:
1234567891234
to
1,234,567,891,234
(this is what is called by commafication of a number)
there is a solution using the lookaround but I tried to solve it without using that. Unfortunately I still not come up with the right command and that's why I am here. Here is my command till now:
:%s/\(\d\)\(\(\d\d\d\)\+\)/\1,\2/gc
which produces:
1,234567891234
The problem is with \+
which takes the longest chain. How can I repeat that for the rest of the chain?
Upvotes: 2
Views: 193
Reputation: 786349
In vim
you can use this regex to insert comma after every 3 digits:
:%s/\v(^)@!((\d{3})+$)@=/,/g
This is assuming each number is on a separate line.
If you have more than one number per line then use:
:%s/\v(<)@!((\d{3})+>)@=/,/g
PS: Tested on VIM - Vi IMproved 7.2
and VIM - Vi IMproved 7.4
Upvotes: 3