My thought was that the pattern would be compared in the order it was compiled in $Date, so if $MMDDYYYY matched $_ in the example the matching should stop and report what was matched.my $dom = qr{0[1-9]|[12][0-9]|3[01]}; my $month = qr{0[1-9]|1[012]}; my $fourYear = qr{2003}; my $twoYear = qr{03}; my $MMDDYYYY = qr{$month$dom$fourYear}; my $DDMMYYYY = qr{$dom$month$fourYear}; my $YYYYMMDD = qr{$fourYear$month$dom}; my $DDMMYY = qr{$dom$month$twoYear}; my $MMDDYY = qr{$month$dom$twoYear}; my $MMDD = qr{($month$dom)}; my $DDMM = qr{$dom$month}; my $Date = qr{($MMDDYYYY|$DDMMYYYY|$YYYYMMDD|$DDMMYY|$MMDDYY|$DDMM +|$MMDD)}; opendir(DIR,".") or die "Cannot open directory for reading: $1\n"; my @files = grep { /^[^\.|\.\.]/ } readdir(DIR); for (@files) { unless ( $_ =~ /($Date)/g) { no_match($_); next; } my $match = "${^N}:$`--$&--$'"; print "In file -->$_<--\n"; print "$1\n"; }
The problem is that for a given line with matches for both $MMDDYYYY and $DDMM the pattern for $DDMM is reported even though the text that matches $MMDDYYYY is first in the line.
If I change $Date to equal just $MMDDYYYY alone the test works correctly and if I change $Date to just $DDMM it returns the appropriate portion. The patterns in $Date are ordered in preference, but it is not returning in that fashion.
My main interest is in discovering if the order of qr() can be different than what was placed in at the compliation of the pattern. It may also be that I have a regex problem, comments on both are welcome.
TIA, Chad.
In reply to qr() match order with multiple patterns by gnu@perl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |