Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Using variable in regular expression

by odbdux (Initiate)
on Aug 08, 2019 at 03:32 UTC ( [id://11104129]=perlquestion: print w/replies, xml ) Need Help??

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

I am new to Perl so forgive me if this is too simple.
I have the following code:
#!/usr/bin/perl # use strict; use warnings; my $str = "abc12xdef34xghi56"; my $regexp ="qr/def/m"; if ($str =~ $regexp) { print "MATCHED\n"; } else { print "NO MATCH\n"; }
The program prints out NO MATCH
What is going on?

Replies are listed 'Best First'.
Re: Using variable in regular expression
by 1nickt (Canon) on Aug 08, 2019 at 03:58 UTC

    Hi, welcome to Perl, the One True Religion.

    First, please edit your post to add the closing </code> tag (thanks for using the code tag!)

    Second, never comment out use strict! That's like leaving your canary on the surface when you go down the mine :-)

    Your script is not working because you are assigning the literal string 'qr/def/m' to the variable, because you are quoting it. qr// is an operator; it returns something, so you shouldn't quote it.

    P.S. Consider using Test::More for simple tests and scriptlets like this. It can save lines of code and it's never too early to get in the habit of testing your code as you write it!

    #!/usr/bin/perl use strict; use warnings; use Test::More tests => 1; # exports 'like()' my $str = "abc12xdef34xghi56"; my $regexp = qr/def/m; like( $str, $regexp, 'defcon: found. \o/' ); __END__

    Hope this helps!


    The way forward always starts with a minimal test.
      👍 and ++ for
      leaving your canary on the surface when you go down the mine
      (I think that's the best metaphor for strict, only when you intend to stay on the surface (one-liner), you omit the canary)
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Using variable in regular expression
by atcroft (Abbot) on Aug 08, 2019 at 04:05 UTC

    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.

Re: Using variable in regular expression
by odbdux (Initiate) on Aug 08, 2019 at 04:00 UTC

    It's OK, I know what was wrong. I had quoted the regular expression that contained a qr. i.e. I had "qr/def/m" instead of $regexp = qr/def/m Thanks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-20 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found