本文介绍了编号的正则表达式捕获的最大数量是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在维护一些通过串行无线电读取值并将它们解压为 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));
}

这个笨拙的捕获正则表达式让我想知道:Perl 中编号捕获变量的上限是多少?它一直到 $MAXINT 吗?

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?

推荐答案

这个脚本至少可以工作到 $N=5000000.之后,它会耗尽内存.

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";

这篇关于编号的正则表达式捕获的最大数量是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 17:08