John M
John M

Reputation: 53

Vim non-greedy search

I am trying to remove everything up to the first comma of each line in a csv file. The lines are like:

-29.45829963684082~149.14680480957031,-000029.45829963684082, 000149.14680480957031,WGS84
-29.46190071105957~149.09509277343750,-000029.46190071105957, 000149.09509277343750,WGS84

When I execute %s/.*,\\{-}// it removes everything!

If I use %s/.*,// it removes all except WGS84 which is what I would expect.

Can anyone tell me what I'm doing wrong? I thought \{-} imposed non-greedy matching in Vim.

Thanks,

John

Upvotes: 3

Views: 1545

Answers (4)

paxdiablo
paxdiablo

Reputation: 881423

I would just use [^,]* instead of .*. That will make your regex non-greedy for commas.

In terms of why your variant isn't working, you seem to have the objects in your regex the wrong way around. {-} is supposed to be equivalent to * but non-greedy, and you're already used * to suck up as much as possible. You also have too many backslashes in there. Try:

:%s/.\{-},//

instead.

When I do that on your test input, I get:

-000029.45829963684082, 000149.14680480957031,WGS84
-000029.46190071105957, 000149.09509277343750,WGS84

as expected.

Upvotes: 3

desimusxvii
desimusxvii

Reputation: 1094

Another route to take is to use the global command.

:g//normal dt,

Upvotes: 2

xdazz
xdazz

Reputation: 160833

\{-} is the non-greedy matching in Vim, so you should use \{-} instead of *.

Try:

%s/.\{-},//

Upvotes: 3

Majid Laissi
Majid Laissi

Reputation: 19788

here's what you need to do

%s/[^,]*//

Upvotes: 1

Related Questions