Advanced Regular Expression Patterns
Named Captures
Named captures make a match easier to read when several pieces are extracted.
Named Captures
named_captures.pl
use strict;
use warnings;
my $code = ;
my $matched = $code =~ /^(?<kind>[a-z]+)-(?<id>\d+)$/;
my $kind = $matched ? $+{kind} : "none";
my $id = $matched ? $+{id} : "none";
print "code=$code\n";
print "matched=$matched\n";
print "kind=$kind\n";
print "id=$id\n";
use strict;
use warnings;
my $code = ;
my $matched = $code =~ /^(?<kind>[a-z]+)-(?<id>\d+)$/;
my $kind = $matched ? $+{kind} : "none";
my $id = $matched ? $+{id} : "none";
print "code=$code\n";
print "matched=$matched\n";
print "kind=$kind\n";
print "id=$id\n";
use strict;
use warnings;
my $code = ;
my $matched = $code =~ /^(?<kind>[a-z]+)-(?<id>\d+)$/;
my $kind = $matched ? $+{kind} : "none";
my $id = $matched ? $+{id} : "none";
print "code=$code\n";
print "matched=$matched\n";
print "kind=$kind\n";
print "id=$id\n";
named-captures
Named captures keep the extracted fields tied to their meaning instead of only their position in the pattern.