7.4. Grouping

One can use the cluster grouping notation ((?: ... )) or the capture grouping notation (( ... )) to group several characters so the entire group can be repeated with +, * or ?.

The difference between clustering and capturing, is that with capturing the Perl interpreter also keeps track of the text matched by the captures in the $1, $2, $3, etc. variables. Clustering as a result is faster and less intrusive.

For example, the following perl program accepts a string as an argument, and checks if it is an entire sentence that starts with "the" and ends with "there":

use strict;
use warnings;

my $string = lc(shift(@ARGV));

if ($string =~ /the(?: +[a-z]+)* +there/)
{
    print "True\n";
}
else
{
    print "False\n";
}

It is possible to nest groupings, so for example the following matches a square brackets-enclosed semicolon separated list of curly braces-enclosed comma-separated lists of numbers:

use strict;
use warnings;

my $string = lc(shift(@ARGV));

if ($string =~ /\[\{[0-9]+(?:,[0-9]+)+\}(?:;\{(?:[0-9]+(?:,[0-9]+)+)\})+\]/)
{
    print "True\n";
}
else
{
    print "False\n";
}

So it matches strings like [{54,129};{236,78}] and [{54,129};{236,78};{78,100,808};{72,1009,8}]


Written by Shlomi Fish