Question

The perl negative look ahead is not working on large strings ( length > 40000, in active perl and cygwin perl, version 5.14 ). I tried the same code with mingw perl 5.8.8 and it stops working for strings with length > 5000.

The code I am using is:

my $str = q(A  B);
my $pattern = '(A)(?:(?!(X)).)*(B)';  
if ( $str =~ m/$pattern/ ) {
    print "matched\n";
}

This works fine for all three versions of the perl. But when I increase the length of the string by adding spaces, the pattern stops matching.

for e.g.: my $str = q(A ...some 50000 spaces... B);

Kindly help.

Was it helpful?

Solution

Perl imposes an internal limit (happens to be a signed 16-bit integer on most systems) on the size of various regex operations to limit stack growth. This answer has a very good breakdown of the limit.

From empirical testing, when the space count gets to 32767, that's when you fail, so it's certainly this limit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top