Saturday 15 August 2015

regex - sed replace "func_name(old_args)" with "func_name()" -


I'm learning to use sed , I sed Investigate and try in many ways, but there are always some scenarios that I fail to cover.

Actually, give me func_name (old_args) to func_name () . old_args can be a lot of things, for example, foo () , foo (argument) , foo-> Ptr , foo_ptr-> Cotent , etc.

The question is, first I ( and next ) .

Any suggestions? Thank you very much

It is a bit difficult to do with regular expressions because the language you try To parse, in fact, is not regular, Sed can not be trusted on this, so we have to take support of Pearl.

Fortunately, Pearl Reggaez can describe more than regular languages; Specifically, they support recycling. You can write

  perl -pe 'BEGIN {$ / = ""; } S / func_name (\ (({^ ([^ ()] | (? 1)) * \)) / func_name () / g 'file name  

and get the following behavior:

  $ cat file foo, func_name (foo), xyzzy, func_name (foo (), bar (hawk () (), qux ()), cooks (), bar (); Buzz () $ perl -pe 'BEGIN {$ / = ""; } S / func_name (\ (([^ ()] | (? 1) * \)) / func_name () / g 'file foo, func_name (), xyzzy, func_name (), bar (); Falcon ()  

BEGIN {$ / = ""; } puts Pearl in slop mode (hence the multilanguage expressions match regex). So the heart of this trick is (\ (([^ ()] | (? 1)) * \)) is the group occupied, and especially (? 1) In that which is recursive.

It actually says: match with a string of an initial bracket ( \ () [^ ()] [[/ />] or Something that matches the regex to group 1 ( (? 1) ), followed by a closing bracket ( \) ) . Since it is itself regzox to capture group 1, it iterates itself and matches some string with the balanced bracket.


No comments:

Post a Comment