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

I'm trying to figure out why this regex matches... Do I need to escape something, or pass a parameter to the regex? Thanks.
#!/usr/bin/perl use strict; my $line = "mdrecoveryd"; my $process = "/bin/bash -c -x /bin/ttrun-parts && /bin/ttrun-parts +/etc/cron.hourly || run-parts /etc/cron.hourly"; if ($line =~ m/$process/i) { print "YES\n"; } else { print "NO\n"; }

Replies are listed 'Best First'.
Re: Regex false match
by GrandFather (Saint) on Dec 19, 2006 at 02:01 UTC

    because || matches. You need to quote the match string if you are trying to match it verbatim:

    if ($line =~ m|\Q$process\Q|i) {

    DWIM is Perl's answer to Gödel
      Many thanks.

        Grandfather meant \Q..\E, not \Q..\Q.

        if ($line =~ m/\Q$process\E/i) {

        That checks if $process is in $line. You could also use index, which might even be faster.

        if (index(lc($line), lc($process)) >= 0)) {

        If you wanted to check if the two vars are the same, then that should be

        if ($line =~ m/^\Q$process\E\z/i) {

        or

        if (lc($line) eq lc($process)) {