google /re2

RE2 is a fast, safe, thread-friendly alternative to backtracking regular expression engines like those used in PCRE, Perl, and Python. It is a C++ library.

★ 9,850⑂ 1,257👁 228⑃ 160● 496▤ 67
C++BSD-3-ClauseCreated 2014-08-18
Download: 2025-11-052025-11-05 ↓052025-11-05
+2
Daily
9,850
stars
1,257
forks
228
watchers
160
prs
496
issues
67
releases

README

RE2, a regular expression library

RE2 is an efficient, principled regular expression library
that has been used in production at Google and many other places
since 2006.

_Safety is RE2's primary goal._

RE2 was designed and implemented with an explicit goal of being able
to handle regular expressions from untrusted users without risk.
One of its primary guarantees is that the match time is linear in the
length of the input string. It was also written with production concerns in mind:
the parser, the compiler and the execution engines limit their memory usage
by working within a configurable budget—failing gracefully when exhausted—and
they avoid stack overflow by eschewing recursion.

It is not a goal to be faster than all other engines under all circumstances.
Although RE2 guarantees a running time that is asymptotically linear in
the length of the input, more complex expressions may incur larger constant factors;
longer expressions increase the overhead required to handle those expressions safely.
In a sense, RE2 is pessimistic where a backtracking engine is optimistic:
A backtracking engine tests each alternative sequentially, making it fast when the first alternative is common.
By contrast RE2 evaluates all alternatives in parallel, avoiding the performance penalty for the last alternative,
at the cost of some overhead. This pessimism is what makes RE2 secure.

It is also not a goal to implement all of the features offered by Perl, PCRE and other engines.
As a matter of principle, RE2 does not support constructs for which only backtracking solutions are known to exist.
Thus, backreferences and look-around assertions are not supported.

For more information, please refer to Russ Cox's articles on regular expression theory and practice:

 Regular Expression Matching Can Be Simple And Fast
 Regular Expression Matching: the Virtual Machine Approach
 Regular Expression Matching in the Wild

 Syntax

In POSIX mode, RE2 accepts standard POSIX (egrep) syntax regular expressions.
In Perl mode, RE2 accepts most Perl operators.  The only excluded ones are
those that require backtracking (and its potential for exponential runtime)
to implement.  These include backreferences (submatching is still okay)
and generalized assertions.
The Syntax wiki page
documents the supported Perl-mode syntax in detail.
The default is Perl mode.

 C++ API

RE2's native language is C++, although there are ports and wrappers listed below.

 Matching Interface

There are two basic operators:
RE2::FullMatch requires the regexp to match the entire input text, and
RE2::PartialMatch looks for a match for a substring of the input text,
returning the leftmost-longest match in POSIX mode and the
same match that Perl would have chosen in Perl mode.

Examples:

cpp
assert(RE2::FullMatch("hello", "h.o"))
assert(!RE2::FullMatch("hello", "e"))

assert(RE2::PartialMatch("hello", "h.o"))
assert(RE2::PartialMatch("hello", "e"))


 Submatch Extraction

Both matching functions take additiona

Top issues

Latest release

2025-11-05 2025-11-05 2025-11-05
Full Changelog: https://github.com/google/re2/compare/2025-08-12...2025-11-05

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON