wyc
wyc

Reputation: 55293

How to modify the following VIM regex to work with white space after commas?

I took this regex from this question: What Vim command(s) can be used to quote/unquote words?

:%s/\([^,]\+\)/"\1"/g

It turns this:

foo,foo bar,bar@foo,foo# bar,bar$ foo#

into this:

"foo","foo bar","bar@foo","foo# bar","bar$ foo#"

I would like to modify the regex to work with this:

foo, foo bar ,bar@foo ,foo# bar, bar$ foo#

turning it into this:

"foo", "foo bar", "bar@foo", "foo# bar", "bar$ foo#"

Any suggestions?

Upvotes: 0

Views: 83

Answers (1)

FDinoff
FDinoff

Reputation: 31439

Something like this would work

:%s/\s*\([^,]\+\)/ "\1"/g | %s/^ //

Match all the leading whitespace before putting it into the capture group. Then put a space before the capture group in quotes. This will put and extra space in the first column so you need to remove that space we use the second substitute command.

Upvotes: 1

Related Questions