in reply to Regex to strip, keep, or add—depending

This really seems like a job for Perl rather than a text editor's internal find/replace, especially since you're playing with parentheses; nested parens are notoriously difficult to regex properly.

The following monstrosity accounts for nested parens, and also isn't limited to the start of line; it tries to avoid messing with things that don't look like function calls. This DOESN'T work on the last example (nested function calls which need to be fixed). I'm sure there are some other quirks to Perl syntax which this doesn't catch.

use Regexp::Common qw/balanced/; while (<DATA>) { chomp; my $bp = qr/$RE{balanced}{-parens=>'()'}{-keep}/; $_ =~ s{ &? # optional & ( # capture either: (?<=&) \w+ # bareword preceeded by & | # or \w+ (?=\s*\() # bareword followed by ( ) # (?: \s* (?=\())? # optional white space, if followed by ( (?: # either: $bp # balanced parens | # or # nothing! ) } # if no second capture, supply an empty () { $1 . ($2 || '()') }exg; print "$_\n"; } __DATA__ &foobar &foobar() foobar() &foobar("asdf") &foobar( test(), qw(etc.)) &foobar( test(test() . test()) ) &foobar("a", 5, qw(dave jen anne matt), map({tr/[a-z]/[A-Z]/; $_;} @mi +xedCase)); &dont_change if not &function() &foobar ('<- white space?') &foobar( &test_nested )

Replies are listed 'Best First'.
Re^2: Regex to strip, keep, or add—depending
by JediWizard (Deacon) on Aug 06, 2005 at 16:46 UTC

    FYI... The example I gave will not have issues with nested parens. * (being greedy) would match the last paren followed by a semi-colon (thus nested parens would not be an issue (unless there were nested parens with in the nested function call)).

    Further proving my point that the perl parser will handle more than you would ever want to try with a regex.

    Update: I am wrong. The regex I posted will actually match the first ) followed by a ;... which will break in a nested function call. However nested parens aside... my original point, that you (porbably) do not want to attempt to account for everything the perl parser will account for, remains valid.


    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol

      I'm in full agreement, it's unlikely that any regex will work in all cases. Mine certainly doesn't. Catching nested parens would be as far as I'd want to take it; if I was doing this for my own purposes, probably not even that far. But I wanted to try it for educational purposes (educating myself, that is).
        PPI