Instead of using the qr// operator to create a regular expression, you quoted it, creating a string "qr/def/m" that you are comparing to $str, which is not found.
Compare:
# Command-line version of OP code # (and added line showing value of $regexp): $ perl -Mstrict -Mwarnings -le 'my $str = "abc12xdef34xghi56"; my $regexp = "qr/def/m"; if ( $str =~ $regexp ) { print "MATCHED"; } else { print qq"NO MATCH"; } print qq"regexp: $regexp";' NO MATCH regexp: qr/def/m
# Command-line version of OP code # (and added line showing value of $regexp), with $regexp corrected: $ perl -Mstrict -Mwarnings -le 'my $str = "abc12xdef34xghi56"; my $regexp = qr/def/m; if ( $str =~ $regexp ) { print "MATCHED"; } else { print qq"NO MATCH"; } print qq"regexp: $regexp";' MATCHED regexp: (?^m:def)
Hope that helps.
In reply to Re: Using variable in regular expression
by atcroft
in thread Using variable in regular expression
by odbdux
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |