Simple compiled regexp not working

U

usenet

Kindly consider the following code which illustrates my question:

#!/usr/bin/perl
use strict; use warnings;

my $line = '08/29/2006 to 08/30/2006';
my $date_rx = qr{m!\d{2}/\d{2}/\d{4}!};

print "regexp matches\n" if $line =~ m!\d{2}/\d{2}/\d{4}!;
print "date_rx matches\n" if $line =~ /$date_rx/;

__END__

The way I read perlop, both print statements should fire, as they
should be doing the exact same match. But I only see the first one.
grrr. This ought to be really simple.
 
U

Uri Guttman

u> Kindly consider the following code which illustrates my question:
u> #!/usr/bin/perl
u> use strict; use warnings;

u> my $line = '08/29/2006 to 08/30/2006';
u> my $date_rx = qr{m!\d{2}/\d{2}/\d{4}!};

ok, this will make you smack your head in a big D'Oh!

why do you think the m!! INSIDE the qr{} means anything special? the m//
is an op and pair of delimiters and not a regex itself. so since m and /
are not metachars (the / is not a { or }) then they are compiled to
themselves as plain chars to match. and they don't match.

u> print "regexp matches\n" if $line =~ m!\d{2}/\d{2}/\d{4}!;
u> print "date_rx matches\n" if $line =~ /$date_rx/;

u> __END__

u> The way I read perlop, both print statements should fire, as they
u> should be doing the exact same match. But I only see the first one.
u> grrr. This ought to be really simple.

it is simple. you missed the part about the regex itself vs the op and
delimiters.

i will now let you smack yourself silly. no charge.

:)

uri
 
U

usenet

Uri said:
it is simple. you missed the part about the regex itself vs the op and
delimiters.

Yup, that's what I missed. Gotta get my head screwed on straight.
i will now let you smack yourself silly. no charge.

Owwww (8^-0)

Thanks - I needed that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top