in reply to How to replace greedy alternation?
/^(?=.*a)(?=.*b)(?=.*c).*d/ or die;
If you want to know the order in which they came,
my %order; @order{qw( a b c d )} = map length, /^(?=(.*?)a)(?=(.*?)b)(?=(.*?)c)(.*?)d/ or die; my @ordered = sort { $order{$a} <=> $order{$b} } keys %order;
This is probably slow for long strings.
Update: Actually, no need for a hash.
my @order = map length, /^(?=(.*?)a)(?=(.*?)b)(?=(.*?)c)(.*?)d/ or die; my @ordered = ( qw( a b c d ) )[ sort { $order[$a] <=> $order[$b] } 0..$#order ];
Update: Back to the practical, using multiple regexp and saving @- should be much faster. Especially if scanning for constants.
my @unordered = qw( a b c d ); my @order; for my $s (@unordered) { push @order, /\Q$s/ ? $-[0] : die; } my @ordered = @unordered[ sort { $order[$a] <=> $order[$b] } 0..$#order ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to replace greedy alternation?
by jwkrahn (Abbot) on Jan 28, 2009 at 06:52 UTC | |
|
Re^2: How to replace greedy alternation?
by Anonymous Monk on Jan 28, 2009 at 07:17 UTC | |
by jwkrahn (Abbot) on Jan 28, 2009 at 07:25 UTC |