Kwahn
Kwahn

Reputation: 476

Stripping function calls from lines using regex

Let's say I have a line,

$bagel = parser(1);
$potato = parser(3+(other var));
$avocado = parser(3-(var1+var2+var3));
$untouchedtoast = donotremove(4);

I want to print, instead of parser(1), just 1. So I want to strip function calls (matching parser(.) I guess?), but leave the innards untouched. The output would, ideally, be

$bagel = 1;
$potato = 3+(other var);
$avocado = 3-(var1+var2+var3);
$untouchedtoast = donotremove(4);

I tried %s/parser(.)//g, but it only replaced everything to the left of the innards. Tried a few other wildcards, but I think I have to somehow pass a variable from the input regex to the output regex, and I'm not sure if that's possible. If it matters, I'm doing this in vim.

Thoughts?

Upvotes: 0

Views: 35

Answers (2)

Bohemian
Bohemian

Reputation: 425003

Try this:

search: \w\(|\)(?=;)
replace: blank

Upvotes: 0

phd
phd

Reputation: 94453

%s/parser(\(.\+\));/\1;/

Search parser();, extract everything inside () using \(.\+\) group, replace the entire expression with the group (\1), add a semicolon (because it was eaten by the search expression).

Upvotes: 2

Related Questions