keerthan kumar
keerthan kumar

Reputation: 391

gvim regexp for nested parentheses

I have file containing abc(de+fgh(2a+2b))+xyz(). i want to write regexp(preferably vim) to get a pattern like de+fgh(2a+2b) + xyz() .

I tried in gvim regexp But while matching parenthesis, if i use greedy option it will match abc(de+fgh(2a+2b))+xyz() and for non-greedy option it will matching with abc(de+fgh(2a+2b')')+xyz() , how to match with abc(de+fgh(2a+2b)')'+xyz().

Regards keerthan

Upvotes: 1

Views: 459

Answers (3)

Alan Gómez
Alan Gómez

Reputation: 378

The following regex match almost all arbitrary nested balanced parenthesis cases:

:%s/\(\(([^)]\+\)\+\()[^(]\+\)\+\)/***&***

Note: Just for experimental use and test

Upvotes: 0

Kent
Kent

Reputation: 195059

I won't do it with regex, assume that your cursor is at BOL, just do:

%di(v0p

you will get desired output.

Translating it into english, take the stuff between first (..) group and concatenate it with whatever after the first (...) group.

You can use :normal cmd or macro to apply the operations on multiple lines.

Upvotes: 1

mjmdavis
mjmdavis

Reputation: 2831

Watch out! regex cannot handle counting arbitrary numbers of brackets. If you want to do this generally, you might need to write a parser.

That said, if you only need this to work for a specific case: http://regexr.com/3fne0

in vim this is:

%s/[^()]*(\([^()]*([^()]*)\))\(.*\)/\1\2/g

Upvotes: 0

Related Questions