PHP preg_match length 3276 limit

It looks like PHP preg_match has a 3276 character limit for matching duplicate characters in some cases.

i.e.

^(.|\s){0,3276}$ works, but ^(.|\s){0,3277}$ does not work.

This does not seem to always apply, since it works /^(.){0,3277}$/ .

I cannot find this anywhere in the PHP documentation or the error debugger. 3276 seems like a bit of a weird border, the only thing I can think of is that it's about 1/10 of 32767, which is the limit for a signed 16-bit integer.

preg_last_error() returns 0.

I reproduced the problem http://www.phpliveregex.com/ as well as my local system and web server.

EDIT: it looks like we are getting a "Warning: preg_match (): compilation failed: the regular expression is too large at an offset of 16 inches from the code, so this seems to be the same problem as the preg_match_all PHP prefix .

However, the regular expression itself is not very large ... Is PHP some kind of extension when you repeat groups that make it too big?

+7
php preg-match
source share
2 answers

To handle Perl-compatible regular expressions, PHP simply bundles a third-party library that takes care of the job. The behavior you describe is actually documented :

The quantifier "*" is equivalent to {0,}, the quantifier "+" to {1,}, and "?" quantifier to {0,1}. n and m are bounded by non-negative integral values ​​less than a given limit defined when perl. This is usually 32766 on the most common platforms.

Thus, there is always a hard limit. Why do your tests show that the PHP limit is 10 times less than typical? I don’t know about it :)

+1
source share

Try using ^(.|\s){0,3276}(.|\s){0,1}$

0
source share

All Articles