RE2 (software)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
RE2
Original authorGoogle
Initial 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]
Repositorygithub.com/google/re2
Pull requests: code-review.googlesource.com
Written inC++
Engine
    Lua error in Module:EditAtWikidata at line 29: attempt to index field 'wikibase' (a nil value).
    Operating systemCross-platform
    TypeRegular expression library
    LicenseBSD

    RE2 is a C++ software library which implements a regular expression engine. 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.[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

    [edit | edit source]

    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

    [edit | edit source]

    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

    [edit | edit source]

    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 | edit source]

    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

    [edit | edit source]

    Lua error in mw.title.lua at line 392: bad argument #2 to 'title.new' (unrecognized namespace name 'Portal').

    References

    [edit | edit source]
    1. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    2. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    3. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    4. ^ a b Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    5. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    6. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    7. ^ a b c Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    8. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    9. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    10. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    11. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).