RE2: linear-time matching bought by dropping backreferences and look-around
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.
At a glance
- What is it?
- google/re2 is a C++ regular expression library built to accept patterns from untrusted users. It guarantees match time linear in the input length, caps memory with a configurable budget, and refuses any construct that needs backtracking.
- Who is it for?
- Use RE2 when patterns or inputs come from people you do not control and a slow match is a denial-of-service risk. Do not use it when your patterns depend on backreferences or look-around, because the library excludes them on principle, and do not expect it to beat a backtracking engine on short, simple expressions.
- Can I use it commercially?
- Yes. BSD-3-Clause is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 55 days ago.
- What is it written in?
- Mainly C++, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 26, 2026, and from our analysis. They are not legal advice.
Star history
- google/re29,856 ★ · +39
| Repository | 2026-10-11 | 2026-11-26 |
|---|---|---|
| google/re2 | 9.8k | 9.9k |
39 Stars in the last 46 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
Linear match time is the guarantee, and everything else is traded for it
RE2 has been used in production at Google and many other places since 2006, and its stated primary goal is safety. It was designed to handle regular expressions from untrusted users without risk. The central guarantee is that match time is linear in the length of the input string.
Two more production concerns shape the code. The parser, the compiler and the execution engines limit their memory usage by working within a configurable budget, and they fail gracefully when it runs out. They also avoid stack overflow by eschewing recursion.
The project sets itself against the backtracking engines inside PCRE, Perl and Python, where a crafted pattern and input can send matching time up exponentially. RE2 closes that door by design rather than by adding a timeout.
RE2 is pessimistic where a backtracking engine is optimistic
The README explains the speed tradeoff in one contrast. A backtracking engine tests each alternative in sequence, which makes it fast when the first alternative is the common one. RE2 evaluates all alternatives in parallel, so the last alternative costs no more than the first, at the price of some overhead. The README calls this pessimism what makes RE2 secure.
It is explicit about what that does not buy. Being faster than all other engines under all circumstances is not a goal. Complex expressions can incur larger constant factors, and longer expressions raise the overhead needed to handle them safely. So the linear bound is asymptotic: a small pattern on short input can run faster in a backtracking engine, and for a fixed expression RE2 guarantees a running-time bound that grows linearly with input length, not a flat cost.
For measuring your own case, the repository ships a benchmark target and a benchlog directory.
Backreferences and look-around are left out because only backtracking can implement them
RE2 does not try to cover everything Perl and PCRE offer. As a matter of principle, it does not support constructs for which only backtracking solutions are known to exist. In practice that removes backreferences and look-around assertions.
The syntax has two modes. In POSIX mode, RE2 accepts standard POSIX (egrep) syntax. In Perl mode, which is the default, it accepts most Perl operators, excluding only those that require backtracking and its potential for exponential runtime. Submatching is still fine, and a pattern like `(\w+):(\d+)` extracts two groups without trouble.
The consequence is a migration check. A codebase that moves from Python's re or PCRE to RE2 must grep its patterns for `\1`-style references and `(?=`-style assertions. The Syntax page on the project wiki documents the Perl-mode syntax in detail, and it is the list to compare against.
FullMatch and PartialMatch share one calling convention with typed submatch arguments
The C++ API has two basic operators. `RE2::FullMatch` requires the regexp to match the entire input. `RE2::PartialMatch` looks for a match in a substring, returning the leftmost-longest match in POSIX mode and the same match Perl would choose in Perl mode.
Both take extra arguments to receive submatches, and an argument can be a `string*`, an integer type, or `absl::string_view*`. RE2 uses the Abseil type instead of std::string_view for historical reasons. A string_view points into the original text, so it is invalid once that text is deleted or out of scope.
// Successful parsing.
int i;
string s;
assert(RE2::FullMatch("ruby:1234", "(\\w+):(\\d+)", &s, &i));
assert(s == "ruby");
assert(i == 1234);
// Fails: "ruby" cannot be parsed as an integer.
assert(!RE2::FullMatch("ruby", "(.+)", &i));An integer argument that fails to parse makes the whole match fail. In the README's overflow example, "ruby:123456789123" does not match because the number cannot be stored in i, even though the pattern itself matched the text.
Compile once into an RE2 object, and silence the parser with RE2::Quiet
Calling the static functions with a string pattern recompiles the regular expression every time. The README's advice is to compile once into an RE2 object and reuse it. After construction, `re.ok()` tells you whether compilation succeeded, and `re.error()` carries the reason when it did not.
The constructor takes an optional second argument for options. `RE2::Quiet` stops the parser from writing error messages to stderr when a pattern fails, which matters when the patterns come from users and failures are routine rather than bugs. The other predefined options are `Latin1`, which disables UTF-8, and `POSIX`, which selects POSIX syntax and leftmost-longest matching.
You can also declare your own `RE2::Options` object and configure it directly. The full option set is documented in the re2/re2.h header, which is also where the README sends you for building argument lists by hand, using RE2 as a lexer, and parsing hex, octal and C-radix numbers.
Code points are matched as-is, so a precomposed u-umlaut will not match u plus a combining mark
RE2 operates on Unicode code points and makes no attempt at normalization. The README's example is exact: the regular expression for ü (U+00FC) does not match the input made of U+0075 followed by U+0308, a u with a combining diaeresis. The two look the same on screen and are different sequences.
The suggested fix is to normalize both the expressions and the input in a preprocessing step before handing them to RE2. The README points to Unicode Technical Report 15 for the general topic and calls normalization a long, involved subject.
This matters most for search over user-typed text from mixed sources, where macOS filenames and web forms may produce different forms of the same character. The Makefile also carries a commented option to build against ICU for full Unicode properties support, enabled with `-DRE2_USE_ICU`. It is off by default, so Unicode property classes depend on how you build.
Building needs a C++17 compiler and Abseil, and the newest tagged release is 2025-11-05
RE2 builds with GNU make, CMake or Bazel. The simplest path in the README is:
make
make test
make benchmark
make install
make testinstallA C++17 compiler and the Abseil library are required. The tests and benchmarks also need GoogleTest and Benchmark. On Linux the README gives `apt install libabsl-dev libgtest-dev libbenchmark-dev`, on macOS `brew install abseil googletest google-benchmark pkg-config-wrapper`, and on Windows `vcpkg install abseil gtest benchmark`.
The repository is not archived. Its last push was on 2026-10-02, and the newest of the listed releases is 2025-11-05, followed by 2025-08-12 and 2025-08-05. Release names are dates, not version numbers, so you pin by date. The top level includes a python directory, and the README mentions ports and wrappers for other languages. The license is BSD-3-Clause.
Editorial conclusion
Use RE2 when patterns or inputs come from people you do not control and a slow match is a denial-of-service risk. Do not use it when your patterns depend on backreferences or look-around, because the library excludes them on principle, and do not expect it to beat a backtracking engine on short, simple expressions. Before adopting it, run your real patterns through the Perl-mode syntax page on the project wiki and list any that fail to compile.
Frequently asked questions
what is google re2
RE2 is a C++ regular expression library from Google that has been used in production since 2006. It is built to handle patterns from untrusted users, with match time linear in the input length.
Is RE2 faster than other regex engines?
Not always. The README says being faster than all other engines under all circumstances is not a goal. It guarantees linear time, but complex expressions can have larger constant factors than a backtracking engine.
What is the RE2 syntax?
RE2 accepts POSIX (egrep) syntax in POSIX mode and most Perl operators in Perl mode, which is the default. Backreferences and generalized assertions are excluded, and the Syntax wiki page documents the Perl-mode details.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/google-re2)Star history chart for your README
[](https://hysenlabs.com/projects/google-re2)Was this useful?