Advanced Regular Expression Patterns
Noncapturing Groups
Noncapturing groups organize alternatives without creating extra capture values.
noncapturing-groups
Use a noncapturing group when grouping controls the pattern but the grouped text is not a value the program needs later.
Noncapturing Groups
noncapturing_groups.pl
Replay: real traced execution (multi-file project)
use strict;
use warnings;
my $word = "color";
my $matched = $word =~ /^col(?:ou)?r$/;
my $status = $matched ? "spelling" : "other";
print "word=$word\n";
print "matched=$matched\n";
print "status=$status\n";
use strict;
use warnings;
my $word = "colour";
my $matched = $word =~ /^col(?:ou)?r$/;
my $status = $matched ? "spelling" : "other";
print "word=$word\n";
print "matched=$matched\n";
print "status=$status\n";
use strict;
use warnings;
my $word = "collar";
my $matched = $word =~ /^col(?:ou)?r$/;
my $status = $matched ? "spelling" : "other";
print "word=$word\n";
print "matched=$matched\n";
print "status=$status\n";
$word ← color, $matched ← (empty), $status ← other
4my $word→ color = "color"; #@word="colour", "collar"5my $matched→ (empty) = $wordcolor =~ /^col(?:ou)?r$/;6my $status→ other = $matched(empty) ? "spelling" : "other";78print "word=$wordcolor\n";9print "matched=$matched(empty)\n";10print "status=$statusother\n";outputword=color matched= status=other
$word ← colour, $matched ← 1, $status ← spelling
4my $word→ colour = "colour";5my $matched→ 1 = $wordcolour =~ /^col(?:ou)?r$/;6my $status→ spelling = $matched1 ? "spelling" : "other";78print "word=$wordcolour\n";9print "matched=$matched1\n";10print "status=$statusspelling\n";outputword=colour matched=1 status=spelling
$word ← collar, $matched ← (empty), $status ← other
4my $word→ collar = "collar";5my $matched→ (empty) = $wordcollar =~ /^col(?:ou)?r$/;6my $status→ other = $matched(empty) ? "spelling" : "other";78print "word=$wordcollar\n";9print "matched=$matched(empty)\n";10print "status=$statusother\n";outputword=collar matched= status=other