André Vermeulen
André Vermeulen

Reputation: 1824

Extract and use a part of string with a regex in GVIM

I've got a string:

doCall(valA, val.valB);

Using a regex in GVIM I would like to change this to:

valA = doCall(valA, val.valB);

How would I go about doing this? I use %s for basic regex search and replace in GVIM, but this a bit different from my normal usages.

Thanks

Upvotes: 2

Views: 662

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545528

You can use this:

%s/\vdoCall\(<(\w*)>,/\1 = doCall(\1,/

\v enables “more magic” in regular expressions – not strictly necessary here but I usually use it to make the expressions simpler. <…> matches word boundaries and the in-between part matches the first parameter and puts it in the first capture group. The replacement uses \1 to access that capture group and insert into the right two places.

Upvotes: 1

Related Questions