Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Using variable in regular expression

by atcroft (Abbot)
on Aug 08, 2019 at 04:05 UTC ( #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
Node Status?
node history
Node Type: note [id://11104133]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2020-09-20 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (120 votes). Check out past polls.

    Notices?