Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Regex to strip, keep, or add—depending

by JediWizard (Deacon)
on Aug 06, 2005 at 15:41 UTC ( [id://481500]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://481500]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-25 19:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found