Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Storing regexps in variables

by pando (Initiate)
on Mar 16, 2007 at 12:49 UTC ( [id://605141]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, I have a problem with regexp and I have no idea how to overcome this. I have some experience with regexp but this is beyond my knowledge. Looks like ++ from my substring is the problem, but still, how can I do this regexp ?
$substr="libstdc++" $string="compat-libstdc++.so.6" print "Found" if ( $string =~ /$substr/ );
I get an error:
Nested quantifiers in regex; marked by <-- HERE in m/compat-libstdc++ +<-- HERE }/ at

Replies are listed 'Best First'.
Re: Storing regexps in variables
by davorg (Chancellor) on Mar 16, 2007 at 12:53 UTC

    A '+' has a special meaning in a regular expression (it means "one or more of the preceding character"). Two of them in succession is a regex syntax error.

    Your problem is that you have regex metacharacters that you want to be treated as normal characters. That is what the \Q escape sequence is for.

    print "Found" if $string =~ /\Q$substr/;
Re: Storing regexps in variables
by Util (Priest) on Mar 16, 2007 at 13:21 UTC

    I think you left out the 'd' in the second line of your example code;
    Did you mean to say $string="compat-libstdc++.so.6"; ?

    The other monks are correct about quotemeta and \Q to fix your regex problem. Also, consider using index, which is designed for just finding one string in another.

    print "Found" if index( $string, $substr ) != -1;

Re: Storing regexps in variables
by andye (Curate) on Mar 16, 2007 at 12:52 UTC
Re: Storing regexps in variables
by pando (Initiate) on Mar 16, 2007 at 20:42 UTC
    Yes indeed, it was a typo error and I missed a 'd' in my message.

    Anyway, thank you information was really helpful and did the trick.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found