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
    push @x, $1 while /(?=((\d)(\d)(?(?{ $3 <= $2 })(?!))))/g;

    Bravo! That's the one I was looking for. I had a vague idea that it could be done within the regex, but I didn't know how to start.

    Of course, I had to study pelre for a half an hour to understand what you'd done. But I learned 3 or 4 new things about Perl on the way. The neat thing is, I'd looked at those parts of perlre before. But I couldn't make heads or tails of them without your example.

    Thanks!

    throop