When you use tracking to see which regex matched does it still compile to one regex internaly
Yes.
not as optimised as a non tracking version but still better than the looping solution ?
It has the same behaviour insofar as at any point during a tracked pattern match, the "degrees of freedom" the engine has available to try is the same as for an ordinary assembled pattern. It's just that the tracked pattern is stuffed full of (?{...}) zero-width eval assertions.
If there are two regexen in the list that match does it return the first, last, all or an indeterminate selection of the above ?
For a given target, the same path through the pattern will always be followed. In that regard it is perfectly deterministic, it is just that it is sometimes hard to determine in advance what that will be. It sort of makes sense if you squint hard enough. Consider:
#! /usr/local/bin/perl -w use strict; use Regexp::Assemble; my $re = Regexp::Assemble->new(track => 1) # remember to double up your backslashes ->add( '^X\\d+' ) ->add( '^X\\d\\d*' ) ->add( '^\\s*X\\d\\d*' ) ->add( '^X\\d\\d' ) ->add( '^X\\d' ) ; while( <DATA> ) { chomp; print $re->matched, " matched <$_>\n" if $re->match($_); } __DATA__ XY1 X234 X56 X4 Z0 X77
This produces:
^X\d\d* matched <X234> ^X\d\d* matched <X56> ^X\d\d* matched <X4> ^\s*X\d\d* matched < X77>
But I would stress above all that the list of patterns in this case is in need of reformulation anyway. Hmm, in fact, this is very interesting. With a suitably exhaustive population of target test stings, you could use this approach to weed out "can't happen" patterns.
Thank-you for asking this question! I might recycle some of the ideas in this thread into examples for the module distribution.
- another intruder with the mooring in the heart of the Perl
In reply to Re^3: Regex and question of design
by grinder
in thread Regex and question of design
by amaguk
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |