Home > Net >  In Perl, is there a limit to the number of capture groups in a Regular Expression?
In Perl, is there a limit to the number of capture groups in a Regular Expression?

Time:12-15

Is there a limit to the number of capture groups in a regular expression? I used to think it was 9 ($1 ... $9), but haven't found anything in the perlre docs to confirm this. And in fact, the following code shows that there are at least 26.

#!/usr/local/bin/perl

use strict;
use warnings;

my $line = " a b c d e f g h i j k l m n o p q r s t u v w x y z ";

my $lp = "(\\w) ";
my $pat = "";
for (my $i=0; $i<26; $i  )
{
   $pat = $pat . $lp;
}

$line =~ /$pat/;
print "$1 $2 $3 $24 $25 $26\n";

Note that this question: How many captured groups are supported by pcre2 substitute function only refers to the PCRE2 C library. I'm asking about Perl.

CodePudding user response:

https://perldoc.perl.org/perlre says:

There is no limit to the number of captured substrings that you may use.

  • Related