Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Perl regular expression question

by tlm (Prior)
on Aug 25, 2005 at 10:33 UTC ( [id://486498]=note: print w/replies, xml ) Need Help??


in reply to Perl regular expression question

In addition to the very helpful comments you've already received, I'd like to point out that, as a rule, I'd "metaquote" the scalar variable in the regex, just in case it contains characters (e.g. +) that otherwise would have special meaning in a regular expression:

m|\Q$input|o

For example:

my $string = '15+2=17'; my $input = '5+2'; $string =~ m|$input|o; # Fails $string =~ m|\Q$input|o; # Succeeds
The first match fails because it tries to match one or more 5s immediately followed by a 2. That's because the + is being interpreted as a regexp quantifier, not a literal character.

See the aforementioned perlre, and while you're at it also check out quotemeta.

the lowliest monk

Replies are listed 'Best First'.
Re^2: Perl regular expression question
by halley (Prior) on Aug 25, 2005 at 13:21 UTC
    Unless of course $input were a stored regular expression (such as qr//), where the matching behavior was expressly desired.

    --
    [ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2025-06-20 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.