RE2 (software)

Last updated
RE2
Original author(s) Google
Initial releaseMarch 11, 2010;15 years ago (2010-03-11) [1]
Stable release
2021-04-01 / August 12, 2025;41 days ago (2025-08-12) [2]
Repository github.com/google/re2
Pull requests: code-review.googlesource.com
Written in C++
Operating system Cross-platform
Type Regular expression library
License BSD
Website github.com/google/re2 OOjs UI icon edit-ltr-progressive.svg

RE2 is a software library which implements a regular expression engine. It uses finite-state machines, in contrast to most other regular expression libraries. RE2 supports a C++ interface.

Contents

RE2 was implemented by Google and Google uses RE2 for Google products. [3] RE2 uses an "on-the-fly" deterministic finite-state automaton algorithm based on Ken Thompson's Plan 9 grep. [4] It is designed to avoid ReDoS (regex denial of service) attacks.

Comparison to PCRE

RE2 performs comparably to Perl Compatible Regular Expressions (PCRE). For certain regular expression operators like | (the operator for alternation or logical disjunction) it is superior to PCRE. Unlike PCRE, which supports features such as lookarounds, backreferences and recursion, RE2 is only able to recognize regular languages due to its construction using the Thompson DFA [4] algorithm. It is also slightly slower than PCRE for parenthetic capturing operations.

PCRE can use a large recursive stack with corresponding high memory usage and result in exponential runtime on certain patterns. In contrast, RE2 uses a fixed stack size and guarantees that its runtime increases linearly (not exponentially) with the size of the input. The maximum memory allocated with RE2 is configurable. This can make it more suitable for use in server applications, which require boundaries on memory usage and computational time.

Adoption

RE2 is available to users of Google Docs and Google Sheets. [5] Google Sheets supports RE2 except Unicode character class matching. [6] RegexExtract does not use grouping.

Example

Here is an example of using re2 against a potential ReDoS (regular expression denial of service) attack.

#include<re2/re2.h>importstd;usingstd::string;usingre2::RE2;intmain(intargc,char*argv[]){stringtext="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!"stringpattern="(a+)+$";boolmatch=RE2::FullMatch(text,pattern);std::println("Match result: {}",match);}

RE2 comes with a built-in Python wrapper, available on Python Package Index (PyPI) as google-re2. [7]

The built-in "regexp" package in Go uses the same patterns and implementation as RE2, though it is written in Go. [8] This is unsurprising, given Go's common staff from the Plan 9 team.

The RE2 algorithm has been rewritten in Rust as the package "regex". CloudFlare's web application firewall uses this package because the RE2 algorithm is immune to ReDoS. [9]

Russ Cox also wrote RE1, an earlier regular expression based on a bytecode interpreter. [10] OpenResty uses a RE1 fork called "sregex". [11]

There is an official Java binding, called RE2J (com.google.re2j). [7]

The following languages have unofficial bindings: [7]

See also

References

  1. Cox, Russ (March 11, 2010). "RE2: a principled approach to regular expression matching". Google Open Source Blog. Retrieved 2020-05-29.
  2. "Releases". Github. Retrieved 2025-09-15.
  3. "Search and use find and replace: Find and replace items using regular expressions". support.google.com. Retrieved 30 November 2024.
  4. 1 2 Cox, Russ. "Regular Expression Matching in the Wild". swtch.com.
  5. "Search and use find and replace" . Retrieved 24 March 2020.
  6. "RegMatch".
  7. 1 2 3 "Github - google/re2". github.com. 1 July 2025.
  8. "regexp package - regexp - Go Packages" . Retrieved 8 Nov 2024.
  9. "Making the WAF 40% faster". The Cloudflare Blog. 1 July 2020.
  10. "Regular Expression Matching: the Virtual Machine Approach". swtch.com.
  11. "openresty/sregex: A non-backtracking NFA/DFA-based Perl-compatible regex engine matching on large data streams". OpenResty. 6 February 2024.