RE2
Original authorGoogle
ReleaseMarch 11, 2010; 16 years ago (2010-03-11)[1]
Stable release
2021-04-01 / August 12, 2025; 10 months ago (2025-08-12)[2]
Written inC++
Operating systemCross-platform
TypeRegular expression library
LicenseBSD
Websitegithub.com/google/re2 Edit this at Wikidata
Repositorygithub.com/google/re2

RE2 is a C++ software library[3] which implements a regular expression engine[3]. It uses finite-state machines, in contrast to most other regular expression libraries. RE2 requires a minimum C++ version of C++17, and uses the Abseil library by Google.

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

Comparison to PCRE

edit

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[5] 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

edit

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

Example

edit

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

import <re2/re2.h>;

import std;

using std::string;
using re2::RE2;

int main(int argc, char* argv[]) {
    string text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!"
    string pattern = "(a+)+$";
    bool match = RE2::FullMatch(text, pattern);
    std::println("Match result: {}", match);
}
edit

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

The built-in regexp package in Go uses the same patterns and implementation as RE2, though it is written in Go.[9] 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.[10]

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

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

The following languages have unofficial bindings:[8]

See also

edit

References

edit
  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. ^ a b Dediu, Adrian-Horia; Martín-Vide, Carlos; Truthe, Bianca (2013-03-15). Language and Automata Theory and Applications: 7th International Conference, LATA 2013, Bilbao, Spain, April 2-5, 2013, Proceedings. Springer. pp. 322–324. ISBN 978-3-642-37064-9.
  4. ^ "Search and use find and replace: Find and replace items using regular expressions". support.google.com. Retrieved 30 November 2024.
  5. ^ a b Cox, Russ. "Regular Expression Matching in the Wild". swtch.com.
  6. ^ "Search and use find and replace". Retrieved 24 March 2020.
  7. ^ "RegMatch".
  8. ^ a b c "Github - google/re2". github.com. 1 July 2025.
  9. ^ "regexp package - regexp - Go Packages". Retrieved 8 Nov 2024.
  10. ^ "Making the WAF 40% faster". The Cloudflare Blog. 1 July 2020.
  11. ^ "Regular Expression Matching: the Virtual Machine Approach". swtch.com.
  12. ^ "openresty/sregex: A non-backtracking NFA/DFA-based Perl-compatible regex engine matching on large data streams". OpenResty. 6 February 2024.

📚 Artikel Terkait di Wikipedia

RE2

RE2 may refer to: RE2 (software), a regular expression software library Resident Evil 2, a 1998 survival horror video game Resident Evil 2 (2019 video

MTR (software)

63.210.29.230 0% 10 10 19 19 19 19 8. t-3-1.bas1.re2.yahoo.com 0% 10 10 19 18 32 106 9. p25.www.re2.yahoo.com 0% 10 10 19 18 19 19 An additional example

ReDoS

the input size. Using the RE2 library by Google for C++: import <re2/re2.h>; import std; using std::string; using re2::RE2; int main(int argc, char* argv[])

Palladyne AI

2022, Sarcos acquired Pittsburgh-based robotics company RE2, Inc. Before its purchase by Sarcos, RE2 developed and produced autonomous and teleoperated mobile

Error recovery control

and software RAID implementations to handle the error if problematic. Generally, Western Digital enterprise drives such as Raptor, Caviar RE2 and RE2-GP

Abseil (C++ libraries)

C++ applications within Google, including the Chromium web browser and the RE2 regular expression engine. Similar to the Boost C++ libraries, Abseil extends

RE Engine

【CAPCOM RE:2019】「バイオハザード RE2」における組み技リターゲット技術 [[CAPCOM RE:2019] Retargeting techniques for grappling techniques in 'Resident Evil RE2']. CAPCOM R&D. October

Blu-ray

Association. Archived from the original on July 1, 2010. Retrieved June 11, 2010. "RE2 Format Specification". Blu-ray Disc Association. Archived from the original