Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
Re: Understanding a portion of perlretutby Athanasius (Archbishop) |
on Dec 09, 2015 at 14:22 UTC ( [id://1149769]=note: print w/replies, xml ) | Need Help?? |
I think the documentation is a little misleading here. At least, it gives me the impression that the first match (if any) is somehow guaranteed to be valid (because codon-aligned). But that’s true only if, as in the example given, the $dna string happens to contain a valid match somewhere — in which case, it will be found first. But if it doesn’t, the first match is an invalid one:
Output:
Adding a \G anchor to the regex:
fixes the results for both dna strings, because \G means Match only at pos() (e.g. at the end-of-match position of prior m//g) (see “Assertions” in perlre), and initially pos() is set at zero. <Begin update> choroba is of course correct, anchoring to the start of the string finds only the first match. But that means that the regex could also be fixed without recourse to \G, by simply anchoring it to the start of the string:
<End update> Perhaps not Perl documentation’s finest hour. :-) Hope that helps,
In Section
Seekers of Perl Wisdom
|
|