In general, I agree with the points in your reply, but a coupla quibbles...
... the modifiers xms ... would be better as:
/atg (.+?) (?>taa|tag|tga)/msx
[emphasis added]
Why better? The modifiers are given in alphabetic order in a Regexp object stringization, but what is the advantage of keeping that order? I thought TheDamian used the //xms ordering throughout PBP simply because it happened to be the order in which those modifiers were introduced and discussed in the regex section of the book, not because of any inherent advantage. Is their order not irrelevant to compilation/execution?
Also, what is the advantage of using atomic grouping for the (?>taa|tag|tga) stop codon (if that's the right terminology) sub-pattern? My understanding is that the primary (maybe the only?) purpose of atomic grouping is to defeat backtracking in situations in which the programmer knows backtracking will impair performance. In the example regex, once the stop codon pattern matches, the overall match succeeds; there is never any backtracking to defeat. (This is already discussed in part here, but I still don't see any advantage.)
... in your real code, unless you're ensuring that $dna is always lowercase (e.g. by using lc), you should also add the i modifier ...
I would emphasize that it's usually very important to ensure lower- (or canonical-) casing for long string matches because the //i modifier can impose a significant performance hit. In the benchmarks I did here, just adding //i to my equivalent of the
/atg(.+?)(?:taa|tag|tga)/
regex incurred a 30% - 35% hit.
In reply to Re^2: Simple regex question. Grouping with a negative lookahead assertion.
by AnomalousMonk
in thread Simple regex question. Grouping with a negative lookahead assertion.
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |