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

$_ =~ s/^&(\w+)([\(\)]*)/$1()/;

The above matches : An amperstand (at the beging of the string/line) followed by one or more letters or numbers (in group 1) folowed by and number of parenthesis (in group 2).

&foobar("asdf") # will only match &foobar(

It appears you are trying to use perl code to modify perl code. This is not usually a safe idea (unless you really really know what you are doing). The Perl parser is very complex, and will account for more things than you are going to want to deal with in a regex.

The having been said... the following I believe will do as close as I can get to what you mean

s/^(&\w+)\(?((?:(?!;).)*)\)?;/$1($2);/

But keep in mind... this will fail in a situation like:

&foobar("a", 5, qw(dave jen anne matt), map({tr/[a-z]/[A-Z]/; $_;} @mi +xedCase));

The perl parser will understand that.... but the supplied regex will not.


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

—Andy Warhol

Replies are listed 'Best First'.
Re^2: Regex to strip, keep, or add—depending
by bradcathey (Prior) on Aug 06, 2005 at 15:53 UTC
    It appears you are trying to use perl code to modify perl code. This is not usually a safe idea (unless you really really know what you are doing). The Perl parser is very complex, and will account for more things than you are going to want to deal with in a regex.

    Actually I'm grepping it in BBEdit's Find and Replace function. Will not be using it in Perl.


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

      I am not familiar with BBEdit, but if you are not using perl... why are you asking the question at perlmonks? regex's in perl may not be the same as regex's in other languages, or other programs.


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

      —Andy Warhol