perlmonkdr has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking to extract the expression that start with the CONSTANT, it could appear in any part and inside of any code, so i placed a couple of example to show you, the only "constant" part is the CONSTANT it self, the other part may vary between situation, let start:

#!/usr/bin/perl #this are a couple of example $_ = <<'REGEXP'; $one = CONSTANT . 'string' . $obj->new[0] . $variable . "string" . $ob +j->method("some".'thing'.$more); $some->method( CONSTANT . $obj->new[0] . 'string' . $obj->method("some".'thing'.$more +). $variable . "string" , "NOT INCLUDED" ); sub( CONSTANT . 'string' . $obj->new[0] . $variable . "string" . $obj->meth +od("some".'thing'.$more) ); REGEXP #let's magic begin! while (/ CONSTANT # begin the search with this string (?> #start looking but not return if fails "[^"]*" # catch every thing within double quotes | # or '[^']*' # catch every thing within single quotes | # or (\( # if its a parentheses (?> #start looking but not return if fails [^()]++ # catch every thing that is not a parentheses | # or (?1) # if its a parentheses make a recursion in it ) \))+ | #or same as above but looking square braket (\[ # if its a square braket (?> #start looking but not return if fails [^\[\]]++ # catch every thing that is not a square brak +et | # or (?2) # if its a square braket make a recursion in it ) \])+ | # or [a-zA-Z0-9_.\-\>\s\$]++ #looking only this string "." is po +int | # or (?R) # if those individual regexp fails, make a recursion, # only stop when all fails )* /xgo) { print "$&\n"; }

The problem with it, is the magic not begin, in fact, don't work at all

What i'm doing wrong?

Replies are listed 'Best First'.
Re: RegExp, the magic not begin
by JavaFan (Canon) on Sep 26, 2010 at 21:05 UTC
    The regexp is too much of a blob to examine in a few minutes (if you want help, comment your code! That includes regexpes). But I did notice [$.a-zA-Z0-9_\-\>\s]. You sure you want to interpolate $. here?

      you right twice, the problem was the dollar sign, i need escape it, now, works like a charm!. millons thanks.

      My bad, you right, I've commented, to make easy discover, what I thinking and how it works, no, i won't interpolate $. variable, I think that it's the dollar sign and a single point.

Re: RegExp, the magic not begin
by Anonymous Monk on Sep 26, 2010 at 19:51 UTC
    What i'm doing wrong?

    You're trying to parse perl as a way to implement a templating engine. There are many mature, well tested and working template engines on CPAN, use one and be done :)

      No, i'm using perl to parse php, often the modules on CPAN aren't prepared to handle all situation, in this case is a simple sucesion of variables and string, i think that i'll cut alot of time with a simple regexp.

        No, i'm using perl to parse php, often the modules on CPAN aren't prepared to handle all situation, in this case is a simple sucesion of variables and string, i think that i'll cut alot of time with a simple regexp.

        Only if you can write it :)