Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the maximum number of numbered regex captures?

Tags:

regex

perl

I'm maintaining some code that reads values over a serial radio and unpacks them into Perl data structures:

# Don't yell at me, I didn't write this
if ($command_string =~
    /^.(.)(.).(..)(.)(..)(.)(....)(....)(....)(....)
        (..)(..)(.)(.)(.)(.)(..)(..)(..)(..)(..)(..)(.)(.).......
            (.)........(.)(.).*/sx) {

    $config->{sequence}      = hex(unpack('H2', $1));
    $config->{radio_id}      = hex(unpack('H2', $2));
    ...
    $config->{radio_type}    = hex(unpack('H2', $26));
    $config->{radio_channel} = hex(unpack('H2', $27));
}

This unwieldy capturing regex made me wonder: what's the upper bound on numbered capture variables in Perl? Does it go all the way up to $MAXINT?

like image 771
wes Avatar asked Aug 24 '12 22:08

wes


1 Answers

This script works up to at least $N=5000000. After that, it runs out of memory.

$N = $ARGV[0] || 5000;
$q = '(.)' x $N;
$regex = qr/$q/;
("x" x $N) =~ $regex;
print eval "\$$N";
like image 149
mob Avatar answered Oct 21 '22 16:10

mob