Perl Compatible Regular Expressions
By Philip Hazel / PCRE Project
Perl Compatible Regular Expressions, or PCRE, is a C library that implements regular expression matching using the same syntax and semantics as the Perl programming language. It gives non-Perl software access to Perl's more expressive…
Definition
Perl Compatible Regular Expressions, or PCRE, is a C library that implements regular expression matching using the same syntax and semantics as the Perl programming language. It gives non-Perl software access to Perl's more expressive regex features, such as lookaheads, lookbehinds, and named capture groups, which the simpler POSIX regular expression standard does not support, and it has become one of the most widely embedded pattern-matching engines in open-source software.
Overview
Regular expressions existed long before PCRE, standardized in POSIX as basic and extended regular expressions, but Perl's own regex engine grew far more expressive over the years, adding constructs that made pattern matching more powerful and more readable for complex cases. Software written in C or C++ that wanted this expressiveness had no way to embed it, since Perl's regex engine was tied to the Perl interpreter itself. PCRE was written to close that gap by re-implementing Perl-style regex matching as a standalone, embeddable C library. Mechanically, PCRE compiles a pattern string into an internal byte-code representation and then executes that byte-code against input text using a backtracking matching algorithm, closely mirroring how Perl's own engine behaves internally. This backtracking approach is what makes advanced features possible: lookahead and lookbehind assertions check context without consuming characters, named capture groups let a match extract labeled substrings, and non-greedy quantifiers control how much text a repeated pattern consumes. A later rewrite, PCRE2, replaced the original library with a cleaner API and better Unicode handling while preserving the same matching semantics. PCRE differs from POSIX regular expressions primarily in expressiveness: POSIX guarantees particular matching semantics (leftmost-longest) and a smaller feature set, while PCRE's backtracking engine supports a much richer pattern language at the cost of predictable worst-case performance on pathological patterns. It is also distinct from newer regex engines like RE2, which trade some Perl-compatible features for guaranteed linear-time matching by avoiding backtracking entirely. In practice, PCRE is embedded inside an enormous number of tools and languages rather than used directly by end users: it powers regex support in PHP, Apache's mod_rewrite, Nginx configuration matching, many text editors, and command-line utilities that offer a "Perl-compatible" regex mode. Developers reach for it whenever they need Perl's advanced pattern syntax inside a non-Perl codebase. The backtracking algorithm that gives PCRE its expressive power is also its main limitation: certain patterns, especially those with nested or ambiguous repetition, can cause catastrophic backtracking, where match time grows exponentially with input length. This makes PCRE risky for matching untrusted, attacker-controlled input without careful pattern review or recursion limits, and it is one of the main reasons engines like RE2 exist as a safer, if less expressive, alternative for security-sensitive contexts. Library maintainers commonly mitigate the risk by imposing match-step or recursion-depth limits, or by running PCRE-based validation behind a timeout, rather than trusting an arbitrary user-supplied pattern to terminate quickly on all inputs.
Key Features
- Implements Perl's regular expression syntax as an embeddable C library
- Supports lookahead and lookbehind assertions for context-aware matching
- Provides named capture groups for readable, labeled pattern extraction
- Compiles patterns into byte-code for repeated matching efficiency
- Offers full Unicode property matching in the PCRE2 revision
- Is embedded inside PHP, Nginx, Apache, and many text editors
- Supports non-greedy and possessive quantifiers for fine-grained matching control
- Maintains close compatibility with Perl's native regex behavior
Use Cases
Alternatives
Frequently Asked Questions
From the Blog
Regular Expressions in Python: A Practical Guide
Regular expressions are one of the most powerful text-processing tools in programming — and one of the most avoided, because the syntax looks intimidating. This guide demystifies regex by building from first principles, with real patterns for emails, phone numbers, dates, and log parsing.
Read More ProgrammingRegular Expressions Explained for Beginners
Regular expressions are compact patterns that search, match, and transform text. Learn the core syntax, common recipes, and how to avoid the classic beginner traps.
Read More ProgrammingRegular Expressions (Regex) for Beginners
Regular expressions are patterns that match, search, and replace text. Learn the core syntax — characters, quantifiers, and groups — with practical examples.
Read More Data ScienceRegular Expressions for Data Cleaning
Learn regular expressions for data cleaning: practical regex patterns analysts use to validate, extract, and standardize messy text data quickly and reliably.
Read More