9

After reading this article about two different types of regular expression algorithms (Perl 5.8.7 and Thompson NFA), the latter being ~1,000,000 times faster than the former, according to the article. I use PHP daily, and use regex quite a lot, so I wanted to know which algorithm PHP uses.

I found this question, however it's only for JavaScript. One of the answers states that JavaScript uses the Thompson NFA algorithm, but that will of course vary from implementation to implementation. I think PHP may have switched to using the faster algorithms when it moved to it's PCRE set of functions, deprecating the ereg_* stuff.

I've looked at the PHP PCRE documentation and, as far as I could see, it tells me nothing as to what algorithm it uses. The acronym PCRE, to me, tells me that it uses Perl Compatible Regular Expressions, so I assume it uses the Perl style algorithm.

Which regular expression algorithm does PHP use? Is it "Perl 5.8.7 style", or does it use the much faster Thompson NFA algorithm, or another one entirely? Could it even use a Perl backend to run it's expressions?

If PHP does use a Perl style algorithm, what exactly is it? I'm looking for an abstract definition/explanation in relation to other algorithms.

5
  • 2
    You're right, PCRE does stand for Perl-compatible regular expressions. However, that's not an algorithm.
    – BoltClock
    Apr 18, 2012 at 22:22
  • 2
    PCRE typically means the pcre library, though, which uses the back-substituting Perl-style algorithms. IIRC the NFA approach only sees big performance gains for typical types of queries on huge datasets, though; 1,000,000 times is a dramatic overstatement for typical applications.
    – Danica
    Apr 18, 2012 at 22:23
  • 1
    Note that the author of that article implemented a library called re2, which uses traditional regular expressions and so is faster on certain inputs, especially large ones. php-re2 is a set of PHP bindings to re2; I have no idea if it's any good.
    – Danica
    Apr 18, 2012 at 22:30
  • 2
    As an aside it would also be interesting to know what algorith MySql uses.
    – Ed Heal
    Apr 18, 2012 at 23:19
  • @Dougal Thank you for the link. From what I've read in the README, it looks very promising, and clean to use.
    – Bojangles
    Apr 18, 2012 at 23:28

1 Answer 1

6

From the manual:

http://www.php.net/pcre:

Regular Expressions (Perl-Compatible)

http://www.php.net/manual/en/intro.pcre.php:

The PCRE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl 5, with just a few differences (see below). The current implementation corresponds to Perl 5.005.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.