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

word
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";
  1. $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
  1. $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
  1. $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