Federico
Federico

Reputation: 799

Vim regex match a space, a number and everything else until ;

I'm trying to make this regex but it's driving me insane. I've strings like this:

foobar 34;lorem ipsum;
foo 34/ABC;dolerm sit;
bar 3445b;amet;

I need to transform them like this:

foobar;34;lorem ipsum;
foo;34/ABC;dolerm sit;
bar;3445b;amet;

The regex I come up to is this one but it matches only numbers: \s\d*; and this one matches the whole line \s\d*\p*; I need something to match only a white space, a number and than everything until the first ";".

Upvotes: 1

Views: 407

Answers (2)

Ben
Ben

Reputation: 8905

You probably could get your original patterns working, if you used "non-greedy" matches, for example \p\{-} for "any number of printable characters, but as few as possible", or by explicitly excluding the ';' character with [^;]* (any number of any character that is not a ';').

:help non-greedy
:help /[ (then scroll down below the E769 topic)

Upvotes: 1

Kent
Kent

Reputation: 195209

does this work for you?

%s/ \ze\d/;/g

if you want to change

foo bar 3 r e p l a c e;bar;

to

foo bar;3;r;e;p;l;a;c;e;bar;

%s/ \d[^;]*/\=substitute(submatch(0)," ",";","g")/

Upvotes: 3

Related Questions