Re: matching patterns - - - EXPLANATION
"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]
Re: matching patterns - - - EXPLANATION
Diego Riano wrote: > I am trying to put it in a better way, (I hope :-)) > > What I am trying to do is check if two patterns could recognize the same > string, for example: > $pattern1=[ag]oprs[cd]x9; > $pattern2=[agpr]oprs; > > these two patterns would recognize, both, a set of strings. however > there will be some strings that only $pattern2 will match and viceversa. > So, How can I compare these two patterns to say if they are similar or > the same? > > thanks again It would be somewhat difficult, I suspect. Perhaps by taking substrings on their canonical forms, after compiling to regex. Unfortunately, matching comparisions: do not guarantee that one is actually a subpattern of the other. You already provided the answer to that question when you indicated that their matching sets each had a subset that was not contained in the union. Unfortunately, a pattern can match another pattern without the other pattern being a sub-pattern: #!perl -w use strict; use warnings; my $pat1 = qr /Hi.*?\sBye/; 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"; } print "\n"; 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 is not a true 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]
Re: matching patterns - - - EXPLANATION
I am trying to put it in a better way, (I hope :-)) What I am trying to do is check if two patterns could recognize the same string, for example: $pattern1=[ag]oprs[cd]x9; $pattern2=[agpr]oprs; these two patterns would recognize, both, a set of strings. however there will be some strings that only $pattern2 will match and viceversa. So, How can I compare these two patterns to say if they are similar or the same? thanks again On Thu, 2003-04-03 at 14:11, Diego Riano wrote: > Hello All > > I am having problem figuring out how to match two patterns. I have two > pattern and I would like to now if they are the same or if one of them > is a sub pattern of the other. Ej: > $pattern1=[agpr]oprs[cd]x9; > $pattern2=[agpr]oprs; > $pattern3=aors; > > $pattern2 is a "subpattern" of $pattern1, and $pattern3 is completely > different form 1 or 2. > > I was trying with If ($paternn1=~/$pattern2/){do somthing . .. } > put it just works if the left side is a string, with a pattern it does > not work > > Some ideas?? > > Thanks in advance > > Diego Riano -- ___ Diego Mauricio Riano Pachon Biologist Institute of Biology and Biochemistry Potsdam University Karl-Liebknecht-Str. 24-25 Haus 20 14476 Golm Germany Tel:0331/977-2809 http://bioinf.ibun.unal.edu.co/~gotem http://www.geocities.com/dmrp.geo/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]