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 ]