in reply to Re^2: Global regexp
in thread Global regexp
It's usually best/simplest to just filter out the bad results.
my @x; while (/(?=((\d)(\d)))/g) { push @x, $1 if $3 > $2; }
You could also do that nicely using a lookup.
my %ok; for my $i (1..8) { for my $j ($i+1..9) { $ok{"$i$j"} = 1; } } my @x = grep $ok{$_}, /(?=(\d\d))/g;
You could build a complex pattern.
use Regexp::List qw( ); my @ok; for my $i (1..8) { for my $j ($i+1..9) { push @ok, "$i$j"; } } my $re = Regexp::List->new()->list2re(@ok); my @x = /(?=($re))/g;
In can be done programmatically in the regexp as well.
my @x; push @x, $1 while /(?=((\d)(\d)(?(?{ $3 <= $2 })(?!))))/g;
(It's pretty crazy that I was able to write the last one without errors and without checking perlre, so you probably shouldn't use that one.)
All tested.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Global regexp
by throop (Chaplain) on Jun 16, 2008 at 22:36 UTC |