Advanced Regular Expression Patterns
Alternation Order
Alternatives are tried left to right, so overlapping alternatives should be ordered deliberately.
Alternation Order
alternation_order.pl
use strict;
use warnings;
my $word = ;
my ($label) = $word =~ /(cat|catalog|dog)/;
$label = defined $label ? $label : "none";
my $exact = $word eq $label ? "yes" : "no";
print "word=$word\n";
print "label=$label\n";
print "exact=$exact\n";
use strict;
use warnings;
my $word = ;
my ($label) = $word =~ /(cat|catalog|dog)/;
$label = defined $label ? $label : "none";
my $exact = $word eq $label ? "yes" : "no";
print "word=$word\n";
print "label=$label\n";
print "exact=$exact\n";
use strict;
use warnings;
my $word = ;
my ($label) = $word =~ /(cat|catalog|dog)/;
$label = defined $label ? $label : "none";
my $exact = $word eq $label ? "yes" : "no";
print "word=$word\n";
print "label=$label\n";
print "exact=$exact\n";
alternation-order
When alternatives overlap, the first successful alternative determines the capture. Put more specific alternatives first when that is the intended result.