Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Storing part of a regex in a variable

by jonneve (Initiate)
on Jul 25, 2014 at 15:37 UTC ( [id://1095057]=perlquestion: print w/replies, xml ) Need Help??

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

Hello all, I'm writing a rather complex script for converting C++ code to Pascal. A number of patterns or sub-pattern occur rather often, and therefore, I'm trying to put them into variables for later use. Here are my variables :
$braces = qr/(?<braces>\{ ([^\{\}] | (?&braces))*? \} )/x; $brackets = qr/(?<brackets>\( ([^\(\)] | (?&brackets))*? \))/x; $identifier = qr/(?<identifier> \w+($brackets)?( \s*(\.|->)(?&identifi +er))?)/;
The $brackets and $braces variables work as I expected, but the $identifier variable doesn't work right, for example :
#This works $meth_impl =~ s/((?<identifier> \w+($brackets)?( \s*(\.|->)(?&identifi +er))?)) \s* \. \s* Trim\(\)/Trim($1)/xg; #This doesn't match anything: $meth_impl =~ s/($identifier) \s* \. \s* Trim\(\)/Trim($1)/xg;
As you can see in the example above, I'm using the $brackets variable in both cases, and it works nicely. Could the problem be that my $identifier variable itself includes the $brackets variable? Do I need to eval it or something to force it to interpolate it correctly? Thanks in advance, Jonathan Neve

Replies are listed 'Best First'.
Re: Storing part of a regex in a variable
by Anonymous Monk on Jul 25, 2014 at 17:30 UTC
    Could the problem be that my $identifier variable itself includes the $brackets variable? Do I need to eval it or something to force it to interpolate it correctly?
    No. No.
    $identifier = qr/(?<identifier> \w+($brackets)?( \s*(\.|->)(?&identifi +er))?)/;

    It seems you forgot /x there. Your last regex (in substitution) looks this when compiled:

    (?^:((?^:(?<identifier> \w+... (etc)

    And

         (?^:

    means

    Starting in Perl 5.14, a "^" (caret or circumflex accent) immediately after the "?" is a shorthand equivalent to "d-imsx".
    So, whitespace in
    <identifier> \w+
    is treated literally. At least, that's what I think, considering the absense of any examples of strings to match.

    Also, stop writing complex regexes on one line. Perl doesn't have to be write-only, you know?

      I can't believe it was so simple! It all makes a lot more sense now! As for the one-liners... you're right, and that's why I was trying to use variables to split things up : it's a lot more readable that way. Thanks for your help!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1095057]
Front-paged by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-24 11:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found