"R. Joseph Newton" wrote:

Sorry Diego,

Turns out that there was a logic error in the code I posted that gave a false positive 
on the match.

> #!perl -w
>
> use strict;
> use warnings;
>
> my $pat1 = qr /Hi.*?\sBye/;
> #  my $pat2 = qr /(Hi|[Hh]ello).*?\sBye--I'll sure miss you\!/;  #  Error

This does not work.  Becuase the space escape is not a ltieral space the smaller regex 
being matched against it wwill actually reject the match.  This is corrected by 
changing the escape here to a literal space:

my $pat2 = qr /(Hi|[Hh]ello).*?\sBye--I'll sure miss you\!/;

> print "$pat1 is the \"smaller\" pattern\n";
> print "$pat2 is the \"larger\" pattern\n";
> print "\n";
>
> if ($pat2 =~ $pat1) {
>  print "$pat2 contains $pat1\n";
> } else {
> #      print "$pat2 matches $pat1\n";     #  False match generator
> }
> print "\n";

Corrected as:

if ($pat2 =~ $pat1) {
 print "$pat2 matches $pat1\n";
} else {
 print "$pat2 does not match $pat1\n";
}
print "\n";

With the corrections noted, the code does provide the same output as the original, 
differing only in the content of the second regex.  I think the following should give 
a pretty accurate picture of whether one compiled regex is a true subpattern of the 
other, although it would take more work to identify subpatterns which originate within 
the larger regex, rather than at the same origin:

> my $test = $pat1;
> chop $test;      # kill  the closing paren of the canonical regex
> if ($test eq substr($pat2, 0, length($test))) {
>   print "$pat1 is a true sub-pattern of $pat2\n";
> } else {
>   print "$pat1 could not be identified by this test as a true initial sub-pattern of 
> $pat2\n";
> }
> print "\n";
>
> my $string1 = "Hello, Bye--I'll sure miss you!";
>
> if ($string1 =~ $pat1) {
>   print "$string1 matches $pat1\n";
> } else {
>   print "$string1 does not match $pat1\n";
> }
>
> if ($string1 =~ $pat2) {
>   print "$string1 matches $pat2\n";
> } else {
>   print "$string1 does not match $pat2\n";
> }
>
> Hi There,podner E:\d_drive\perlStuff>pat_holds.pl
> (?-xism:Hi.*?\sBye) is the "smaller" pattern
> (?-xism:(Hi|[Hh]ello).*?\sBye--I'll sure miss you!) is the "larger" pattern
>
> (?-xism:(Hi|[Hh]ello).*?\sBye--I'll sure miss you!) matches (?-xism:Hi.*?\sBye)
>
> (?-xism:Hi.*?\sBye) is not a true sub-pattern of (?-xism:(Hi|[Hh]ello).*?\sBye--
> I'll sure miss you!)
>
> Hello, Bye--I'll sure miss you! does not match (?-xism:Hi.*?\sBye)
> Hello, Bye--I'll sure miss you! matches (?-xism:(Hi|[Hh]ello).*?\sBye--I'll sure
>  miss you!)
>
> Hi There,podner E:\d_drive\perlStuff>
>
> Joseph
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to