Consider this script, which is based on the answer to
SO 267399 on parsing Roman numbers, although parsing Roman numbers is related to this question.
use warnings;
use strict;
my $qr1 = qr/(?i:M{1,3})/;
my $qr2 = qr/(?i:C[MD]|D?C{1,3})/;
my $qr3 = qr/(?i:X[CL]|L?X{1,3})/;
my $qr4 = qr/(?i:I[XV]|V?I{1,3})/;
print "1000s: $qr1\n";
print " 100s: $qr2\n";
print " 10s: $qr3\n";
print " 1s: $qr4\n";
my $qr = qr/\b((?:$qr1$qr2?$qr3?$qr4?)|(?:$qr2$qr3?$qr4?)|(?:$qr3$qr4?)|(?:$qr4))\b/;
print " Full: $qr\n";
while (<>)
{
chomp;
print " Line: [$_]\n";
while ($_ =~ m/$qr/g)
{
print "Match: [$1] found in [$_] using qr//\n";
}
}
In the data file below, the first three lines contain the Roman number.
mix in here
no mix in here
mmmcmlxxxix
minimum
When starting with built-in Perl 5.22.0 on mac, macOS Sierra 10.12.4 now runs, I get this output (but the Perl version is not critical):
1000s: (?^:(?i:M{1,3}))
100s: (?^:(?i:C[MD]|D?C{1,3}))
10s: (?^:(?i:X[CL]|L?X{1,3}))
1s: (?^:(?i:I[XV]|V?I{1,3}))
Full: (?^:\b((?:(?^:(?i:M{1,3}))(?^:(?i:C[MD]|D?C{1,3}))?(?^:(?i:X[CL]|L?X{1,3}))?(?^:(?i:I[XV]|V?I{1,3}))?)|(?:(?^:(?i:C[MD]|D?C{1,3}))(?^:(?i:X[CL]|L?X{1,3}))?(?^:(?i:I[XV]|V?I{1,3}))?)|(?:(?^:(?i:X[CL]|L?X{1,3}))(?^:(?i:I[XV]|V?I{1,3}))?)|(?:(?^:(?i:I[XV]|V?I{1,3}))))\b)
Line: [mix in here]
Match: [mix] found in [mix in here] using qr//
Line: [no mix in here]
Match: [mix] found in [no mix in here] using qr//
Line: [mmmcmlxxxix]
Match: [mmmcmlxxxix] found in [mmmcmlxxxix] using qr//
Line: [minimum]
The only part of the result that I don’t understand is carriage ^c
(?^:…).
Perl
perlre
perlref
perlop
'Regex quot-like operator',
. ( , SO, . (?^: , .)
: