4

I have lines like this:

$params["request_id_$i"] => $v["request_id_$i"];
$params["drug_name_$i"] => $v["drug_name"];

I want to replace all _ occuring after "=>" with "-". I am able to replace single "_" with following:

s/^.*=> \zs\(.\{-}\)_\(.\{-}\)$/\1-\2/g

But how to do it for multiple _ ?

2 Answers 2

6

The simplest way is to use a lookbehind assertion

s/\(=>.*\)\@<=_/-/g

This is telling vim to check backwards for => for each possible _ match.

2

It's not elegant, but it works:

:%s/=> \zs\(.*\)/\=substitute(submatch(1), '_', '-', 'g')/

I'm sure someone more talented with regexp than I am can find a way that doesn't require the use of a submatch and a second substitute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.