in reply to Problem using value of variable in a regular expression
I guess you've understood this, but your attempt to escape the star didn't work:
$ perl -wE 'say "\*E"' *E
The double-quoted string ate the backslash, the regex just sees *E.
There are several solutions:
# ugly, but works: my $failmsg = "\\*E"; if (/$failmsg/) { ... } # nicer, since you don't have to do the escaping yourself $failmsg = '*E'; if (/\Q$failmsg\E/) { ... } # if you want to quote regular expressions, do this: $failmsg = qr{\*E}; if ($_ =~ $failmsg) { ... }
This last solution is nice because it accepts modifiers, so for example if you want to do a case-insensitive match, you can write $failmsg = qr{\*E}i;.
|
|---|