Other meanings of Perl Compatible Regular Expressions
Computing
Perl Compatible Regular Expressions (PCRE) is a regular expression library originally written in Perl by Philip Hazel, providing a syntax and semantics compatible with the Perl programming language's regular expression engine. It is widely used in many software tools and programming languages, including PHP, Apache, and Nginx, as a faster and more feature-rich alternative to POSIX regular expressions.
PCRE was first released in 1997 by Philip Hazel, a computer scientist at the University of Cambridge, as a library that could be used in non-Perl programs while retaining the expressive power of Perl's regex engine.1 Hazel's goal was to provide a robust, portable, and efficient implementation that could be embedded in C programs. The library quickly gained popularity because it offered advanced features like lookahead, lookbehind, non-capturing groups, and atomic grouping, which were absent from older regex standards such as POSIX. Over the years, PCRE has been maintained by a team of volunteers and has undergone several major revisions, including a complete rewrite (PCRE2) in 2014 to improve performance, Unicode support, and API design.2 Today, PCRE2 is the recommended version, while the original PCRE is still used in legacy systems.
PCRE provides a rich set of features that extend beyond basic regex: backreferences, named capturing groups, lookahead and lookbehind assertions (both positive and negative), atomic groups, possessive quantifiers, and conditional patterns.3 It supports Unicode properties and character classes, allowing matching of scripts, categories, and grapheme clusters. PCRE can also operate in two modes: interpreted (just-in-time compilation) and compiled (pre-compiled patterns). The library includes functions for substring extraction, pattern splitting, and global matching. PCRE2 improved on the original by adding a more efficient JIT compiler, support for 16-bit and 32-bit character strings, and a more user-friendly error reporting system. Its syntax closely follows Perl's, but PCRE has its own extensions and does not always match Perl's latest regex additions.
PCRE is one of the most widely used regex libraries in the world, embedded in hundreds of software projects. Notable users include the PHP programming language (where it replaced the POSIX regex functions), the Apache HTTP Server (mod_rewrite, mod_proxy), the Nginx web server, and the database system MySQL (for the REGEXP operator).4 It is also used in text editors like Sublime Text and Notepad++, and in security tools such as Snort (intrusion detection) and Wireshark (packet analysis). The library's portability—it compiles on virtually all Unix-like systems and Windows—has contributed to its ubiquity. PCRE's influence extends to other languages: many regex implementations in Python, JavaScript, and Ruby have borrowed features from PCRE, though they are not direct ports.
Despite its name, PCRE is not a direct translation of Perl's regex engine; it is a separate implementation that aims for compatibility, but differences exist, particularly in the handling of backreferences in alternation and some Unicode edge cases.5 A lesser-known feature is the \K escape (keep-out), which causes the text matched so far to be excluded from the final match, useful for lookbehind alternatives. PCRE also supports callout functions, allowing a user-written C function to be called during matching, enabling dynamic pattern behavior. The library includes a debugging mode that prints the internal state machine. An often-overlooked tool is pcretest, a command-line utility distributed with PCRE for testing patterns. In 2021, a security vulnerability in PCRE (CVE-2021-33390) was found in the handling of invalid UTF-8, leading to a denial of service; this was quickly patched in PCRE2.
Help improve the encyclopedia. Reports go straight to the site manager.