PCRE 2

PHP’s preg_*() functions use the Perl Compatible Regular Expressions (PCRE) library. This library implements regular expression pattern matching using the same syntax and semantics as Perl 5. It is used by many Open Source projects such as Apache, Exim, and PHP as well as many commercial products such as Apple Safari.

A couple of years ago, the PCRE project redesigned their library’s API and reimplemented large parts of its functionality. These changes were so significant that they decided to skip the version number 9 and went directly from PCRE 8.x to PCRE2 10.0. Notice the “2” which they added to the name of the library to make it clear that PCRE2 should be considered a new library.

PCRE2 has a more explicit pattern interpreter and patterns are checked more aggressively for their validity. The new version has improved error handling capabilities which PHP can leverage to provide more meaningful error messages. Depending on patterns and input, PCRE2 can be faster than PCRE.

The syntax for regular expressions has been expanded for PCRE2. Discussing these syntax additions is, however, outside of the scope of this book. The relevant information can be found in the PCRE2 documentation.