Athanasius has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
Please see update below.While doing pattern matches using qr// with modifiers added, I ran into behaviour I didn’t expect. The following code, using the 's' modifier to
“change "." to match any character whatsoever, even a newline, which normally it would not match.” (perlre: Modifiers)illustrates what I mean:
#! perl use strict; use warnings; my $text = <<EOT; The quick brown fox jumps over the unfortunate dog. EOT my %searches = ( 'A' => 'fox.+?jumps', 'B' => '(?s)fox.+?jumps' ); my %functions = ( '1. no' => \&no_mod, '2. qr//' => \&qr_mod, '3. late' => \&late_mod, '4. inline' => \&inline_mod ); foreach my $search_key (sort keys %searches) { print "\nSearching on '$searches{$search_key}':\n\n"; foreach my $fn_key (sort keys %functions) { printf "%s%-9s 's' modifier: %s\n", $search_key, $fn_key, $functions{$fn_key}->($text, $searches{$search_key}) ? + 'Match found' : 'No match'; } } sub no_mod # no 's' modifier { my ($string, $pattern) = @_; my $regex = qr/$pattern/; return ($string =~ /$regex/); } sub qr_mod { my ($string, $pattern) = @_; my $regex = qr/$pattern/s; # <= 's' modifier return ($string =~ /$regex/); } sub late_mod { my ($string, $pattern) = @_; my $regex = qr/$pattern/; return ($string =~ /$regex/s); # <= 's' modifier } sub inline_mod { my ($string) = @_; return ($string =~ /fox.+?jumps/s); # <= 's' modifier } __END__
This is the output I get:
Searching on 'fox.+?jumps': A1. no 's' modifier: No match A2. qr// 's' modifier: Match found A3. late 's' modifier: No match A4. inline 's' modifier: Match found Searching on '(?s)fox.+?jumps': B1. no 's' modifier: Match found B2. qr// 's' modifier: Match found B3. late 's' modifier: Match found B4. inline 's' modifier: Match found
(I’m running DWIM/Strawberry perl 5.14.2 on Vista 32-bit, and I get the same result with perl 5.10.1 on Cygwin.)
All the results are as expected, except for A3. I can’t see any (logical) difference between the match patterns in late_mod() and inline_mod(), yet A4 matches (as expected) but A3 does not.
I’ve looked at perlop: Regexp Quote Like Operators, also perlfaq6: I'm having trouble matching over more than one line. What's wrong? and How do I match a regular expression that's in a variable? , , but I haven’t found anything that addresses this particular issue.
So my questions are:
Since posting, I’ve found How do I apply switches like /i or /g to a qr regexp? which addresses this issue. But I would still appreciate any further information or clarification.
Thanks,
Athanasius <°(((>< contra mundum
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Pattern matching with qr// and modifiers
by jwkrahn (Abbot) on May 05, 2012 at 04:39 UTC | |
by Athanasius (Archbishop) on May 05, 2012 at 05:56 UTC | |
by JavaFan (Canon) on May 05, 2012 at 09:47 UTC | |
by Athanasius (Archbishop) on May 05, 2012 at 10:06 UTC | |
by sauoq (Abbot) on May 05, 2012 at 12:37 UTC | |
by AnomalousMonk (Archbishop) on May 05, 2012 at 19:29 UTC |