RE2 Documentation

repository·main·Indexed 27 days ago

https://github.com/google/re2

An efficient regular expression library designed for safety, guaranteeing linear-time matching relative to input length to prevent exponential backtracking and stack overflows. Includes guides for installation via GNU Make, CMake, and Bazel, as well as C++ usage for FullMatch, PartialMatch, submatch extraction, and pre-compilation.

Tokens
1.3K
Snippets
7
Records
8
Agent score
45%

What's inside RE2

  1. Install RE2 using Bazel

    main

    Bazel handles dependencies automatically. It is recommended to use Bazelisk to manage Bazel versions.

    go install github.com/bazelbuild/bazelisk@latest
    # or on mac: brew install bazelisk
    
    bazelisk build :all
    bazelisk test :all
  2. Install RE2 using CMake

    main

    If the standard Makefile has trouble finding dependencies, you can use CMake. You can enable tests and benchmarks using -DRE2_TEST=ON and -DRE2_BENCHMARK=ON. You can also enable ICU support with -DRE2_USE_ICU=ON to extend property names available in \p and \P patterns.

    rm -rf build
    cmake -DRE2_TEST=ON -DRE2_BENCHMARK=ON -S . -B build
    cd build
    make
    make test
    make install
  3. Pre-compile RE2 regular expressions

    main

    To avoid recompiling a regular expression on every call, compile it once into an RE2 object and reuse that object for subsequent matches.

    RE2 re("(\\w+):(\\d+)");
    assert(re.ok());  // compiled; if not, see re.error();
    
    assert(RE2::FullMatch("ruby:1234", re, &s, &i));
    assert(RE2::FullMatch("ruby:1234", re, &s));
    assert(RE2::FullMatch("ruby:1234", re, (void*)NULL, &i));
    assert(!RE2::FullMatch("ruby:123456789123", re, &s, &i));
  4. Extract submatches in C++

    main

    Both RE2::FullMatch and RE2::PartialMatch accept additional arguments to store submatches. Supported types for these arguments include std::string*, integer types, or absl::string_view*. Note that absl::string_view is a pointer to the original input text and does not carry its own storage; ensure the original text remains in scope while using it.

    // 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));
    
    // Success; does not extract the number.
    assert(RE2::FullMatch("ruby:1234", "(\\w+):(\\d+)", &s));
    
    // Success; skips NULL argument.
    assert(RE2::FullMatch("ruby:1234", "(\\w+):(\\d+)", (void*)NULL, &i));
    
    // Fails: integer overflow keeps value from being stored in i.
    assert(!RE2::FullMatch("ruby:123456789123", "(\\w+):(\\d+)", &s, &i));
  5. Use RE2 matching interfaces in C++

    main

    RE2 provides two primary matching operators:

    • RE2::FullMatch: Requires the regular expression to match the entire input text.
    • RE2::PartialMatch: Looks for a match for a substring of the input text, returning the leftmost-longest match in POSIX mode or the Perl-equivalent match in Perl mode.
    assert(RE2::FullMatch("hello", "h.*o"))
    assert(!RE2::FullMatch("hello", "e"))
    
    assert(RE2::PartialMatch("hello", "h.*o"))
    assert(RE2::PartialMatch("hello", "e"))
  6. Configure RE2 options

    main

    The RE2 constructor accepts an optional second argument to modify default behavior. Common predefined options include:

    • RE2::Quiet: Silences error messages printed to stderr when a regex fails to parse.
    • RE2::Latin1: Disables UTF-8.
    • RE2::POSIX: Uses POSIX syntax and leftmost-longest matching.

    You can also create and configure a custom RE2::Options object.

    RE2 re("(ab", RE2::Quiet);  // don't write to stderr for parser failure
    assert(!re.ok());  // can check re.error() for details