最新的Perl不能匹配某些正则表达式,且长度不能超过32768个字符 [英] latest Perl won't match certain regexes more than 32768 characters long

查看:122
本文介绍了最新的Perl不能匹配某些正则表达式,且长度不能超过32768个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望一些Perl专家可以对以下内容发表意见.这是我能找到的最小的例子,重现了我的问题:

I am hoping some Perl gurus can opine on the following. This is the smallest possible example I could find that reproduces my problem:

>./perl -e 'print (("a".("f"x32767)."a") =~ /a(?:[^a]|bb)*a/)'
1

但是

>./perl -e 'print (("a".("f"x32768)."a") =~ /a(?:[^a]|bb)*a/)'
>

我确实从源代码编译了最新的Perl,只是看它是否可以解决问题:

and I did compile the latest Perl from source, just to see if it would fix the problem:

>./perl -v

This is perl 5, version 20, subversion 1 (v5.20.1) built for i686-linux

这是一个错误(对我来说很像)吗?

Is this a bug (looks like to me)?

推荐答案

添加use warnings:

use strict;
use warnings;
use feature qw(say);

say "Version is $^V";

say +("a".("f"x32767)."a") =~ /a(?:[^a]|bb)*a/ ? 'matches' : 'no match';

say +("a".("f"x32768)."a") =~ /a(?:[^a]|bb)*a/ ? 'matches' : 'no match';

输出:

Complex regular subexpression recursion limit (32766) exceeded at e.pl line 7.
Complex regular subexpression recursion limit (32766) exceeded at e.pl line 9.
Version is v5.20.1
matches
no match

来自 perldiag

超出了常规规则子表达式递归限制(%d)

(W regexp)在需要回溯的复杂情况下,正则表达式引擎使用递归.递归深度限制为32766,或者在堆栈无法任意增长的体系结构中,递归深度可能会更少. (简单"和中等"情况无需递归处理,也不受限制.)尝试缩短正在检查的字符串;在Perl代码中循环(例如使用while),而不是在正则表达式引擎中循环;或重写正则表达式,使其更简单或减少回溯. (有关掌握正则表达式的信息,请参见perlfaq2.)

Complex regular subexpression recursion limit (%d) exceeded

(W regexp) The regular expression engine uses recursion in complex situations where back-tracking is required. Recursion depth is limited to 32766, or perhaps less in architectures where the stack cannot grow arbitrarily. ("Simple" and "medium" situations are handled without recursion and are not subject to a limit.) Try shortening the string under examination; looping in Perl code (e.g. with while ) rather than in the regular expression engine; or rewriting the regular expression so that it is simpler or backtracks less. (See perlfaq2 for information on Mastering Regular Expressions.)

这篇关于最新的Perl不能匹配某些正则表达式,且长度不能超过32768个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆