What does the caret mean in (? ^: ...) in the string form Perl qr // Regex?

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.

#!/usr/bin/env perl
#
# Based on answer to SO 0026-7399

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";

# This $qr is too simple — it matches the empty string
#my $qr = qr/($qr1?$qr2?$qr3?$qr4?)/;

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, . (?^: , .)

:

  • (?^:…) qr//?
  • , qr//?
+6
2

, ( , -). , qr - (?-ismx:, , Perl, , m ade .

http://perldoc.perl.org/perlre.html#Extended-Patterns:

Perl 5.14, "^" ( ) "?" d-imnsx. ( "d" ) , . .

+8

" ( i, s) ",

$ perl -le'my $re = "a"; for (qw( a A )) { print "$_: ", /$re/i ? "match" : "no match"; }'
a: match
A: match

$ perl -le'my $re = "(?^:a)"; for (qw( a A )) { print "$_: ", /$re/i ? "match" : "no match"; }'
a: match
A: no match

, qr//.

$ perl -le'my $re = qr/a/; print $re; for (qw( a A )) { print "$_: ", /$re/i ? "match" : "no match"; }'
(?^:a)
a: match
A: no match

$ perl -le'my $re = qr/a/i; print $re; for (qw( a A )) { print "$_: ", /$re/i ? "match" : "no match"; }'
(?^i:a)
a: match
A: match
+5

All Articles