Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Using variable in regular expression

by atcroft (Abbot)
on Aug 08, 2019 at 04:05 UTC ( [id://11104133]=note: print w/replies, xml ) Need Help??


in reply to Using variable in regular expression

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found