Subject: | num roman not greedy unless anchored? |
This confused me...
$ perl -MRegexp::Common=number -E 'for (@ARGV) { if (m/$RE{num}{roman}{-keep}/) { say "Found: $1"; } else { say "No Roman Found" }; };' I II III IV IIII
Found: I
Found: II
Found: III
Found: I
Found: III
Seems that if I anchor the regular expression with ^ and $ then it works.
$ perl -MRegexp::Common=number -E 'for (@ARGV) { if (m/^$RE{num}{roman}{-keep}$/) { say "Found: $1"; } else { say "No Roman Found" }; };' I II III IV IIII
Found: I
Found: II
Found: III
Found: IV
No Roman Found
I've attached a more comprehensive test case.
Is this expected?
Subject: | regexp_common_num_roman_not_greedy.t |
use strict;
use warnings;
use Test::More;
use Regexp::Common qw/number/;
note 'with ^ and $ anchors';
for my $roman (qw(I II III IV V VI VII VIII IX X)) {
if ($roman =~ m/^$RE{num}{roman}{-keep}$/) {
is $roman, $1, "$roman => $1";
}
}
note 'without ^ and $ anchors';
for my $roman (qw(I II III IV V VI VII VIII IX X)) {
if ($roman =~ m/$RE{num}{roman}{-keep}/) {
is $roman, $1, "$roman => $1";
}
}
note 'with ^ anchor only';
for my $roman (qw(I II III IV V VI VII VIII IX X)) {
if ($roman =~ m/^$RE{num}{roman}{-keep}/) {
is $roman, $1, "$roman => $1";
}
}
note 'with $ anchor only';
for my $roman (qw(I II III IV V VI VII VIII IX X)) {
if ($roman =~ m/$RE{num}{roman}{-keep}$/) {
is $roman, $1, "$roman => $1";
}
}
done_testing();