Convert Figma logo to code with AI

google logore2

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.

8,886
1,119
8,886
3

Top Related Projects

47,483

ripgrep recursively searches directories for a regex pattern while respecting your gitignore

High-performance regular expression matching library

regular expression library

Quick Overview

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 that provides a regular expression API and implements regular expression matching using a finite-state machine approach, which guarantees linear-time performance.

Pros

  • Fast and efficient, with linear-time performance guarantees
  • Thread-safe and suitable for concurrent use
  • Memory-safe, avoiding stack overflows and other issues common in backtracking engines
  • Supports most Perl-style regular expression syntax

Cons

  • Lacks support for some advanced features found in other regex engines (e.g., backreferences, lookahead)
  • May require more memory than backtracking engines for complex patterns
  • Learning curve for C++ developers not familiar with the library's API
  • Limited language bindings compared to some other regex libraries

Code Examples

  1. Basic pattern matching:
#include <re2/re2.h>
#include <iostream>

int main() {
    RE2 pattern("\\w+@\\w+\\.com");
    std::string text = "Contact us at info@example.com";
    
    if (RE2::PartialMatch(text, pattern)) {
        std::cout << "Email found!" << std::endl;
    }
    return 0;
}
  1. Extracting matched groups:
#include <re2/re2.h>
#include <iostream>

int main() {
    std::string text = "Date: 2023-04-15";
    int year, month, day;
    
    if (RE2::PartialMatch(text, "Date: (\\d{4})-(\\d{2})-(\\d{2})", &year, &month, &day)) {
        std::cout << "Year: " << year << ", Month: " << month << ", Day: " << day << std::endl;
    }
    return 0;
}
  1. Global replacement:
#include <re2/re2.h>
#include <iostream>

int main() {
    std::string text = "The quick brown fox jumps over the lazy dog";
    RE2::GlobalReplace(&text, "\\b\\w{4}\\b", "****");
    std::cout << "Censored: " << text << std::endl;
    return 0;
}

Getting Started

To use RE2 in your C++ project:

  1. Install RE2 (e.g., sudo apt-get install libre2-dev on Ubuntu)
  2. Include the RE2 header in your source file: #include <re2/re2.h>
  3. Compile with the RE2 library: g++ -std=c++11 your_file.cpp -lre2

Example usage:

#include <re2/re2.h>
#include <iostream>

int main() {
    RE2 pattern("Hello, (\\w+)!");
    std::string text = "Hello, World!";
    std::string name;
    
    if (RE2::PartialMatch(text, pattern, &name)) {
        std::cout << "Matched name: " << name << std::endl;
    }
    return 0;
}

Competitor Comparisons

47,483

ripgrep recursively searches directories for a regex pattern while respecting your gitignore

Pros of ripgrep

  • Faster performance for searching large codebases
  • Built-in support for various file types and gitignore rules
  • User-friendly command-line interface with colored output

Cons of ripgrep

  • Limited to text search and not a general-purpose regex library
  • Less flexibility for complex pattern matching compared to RE2

Code Comparison

RE2:

#include <re2/re2.h>
RE2 pattern("\\w+");
RE2::FullMatch(text, pattern);

ripgrep:

use grep_regex::RegexMatcher;
let matcher = RegexMatcher::new(r"\w+").unwrap();
matcher.is_match(text.as_bytes()).unwrap()

Key Differences

RE2 is a C++ library focused on efficient regular expression matching, while ripgrep is a command-line search tool written in Rust. RE2 provides a more comprehensive regex engine suitable for various applications, whereas ripgrep excels in fast, user-friendly code searching.

RE2 offers better support for complex pattern matching and can be integrated into larger applications. ripgrep, on the other hand, is optimized for searching through large codebases quickly and provides a more intuitive interface for developers working directly from the command line.

High-performance regular expression matching library

Pros of Hyperscan

  • Designed for high-performance, multi-pattern matching
  • Supports advanced features like stream matching and vectorized processing
  • Optimized for Intel architectures, leveraging hardware acceleration

Cons of Hyperscan

  • Limited platform support (primarily x86)
  • More complex API and usage compared to RE2
  • Larger memory footprint for pattern compilation

Code Comparison

RE2:

re2::RE2 pattern("\\w+");
re2::StringPiece input("Hello, world!");
re2::StringPiece match;
while (re2::RE2::FindAndConsume(&input, pattern, &match)) {
    std::cout << match << std::endl;
}

Hyperscan:

hs_database_t *database;
hs_compile_error_t *compile_err;
hs_compile("\\w+", 0, HS_FLAG_DOTALL, NULL, &database, &compile_err);
hs_scratch_t *scratch;
hs_alloc_scratch(database, &scratch);
hs_scan(database, "Hello, world!", 13, 0, scratch, onMatch, NULL);
hs_free_scratch(scratch);
hs_free_database(database);

Both RE2 and Hyperscan are powerful regular expression engines, but they cater to different use cases. RE2 focuses on simplicity and safety, while Hyperscan prioritizes high-performance matching for large sets of patterns. RE2 is more portable and easier to use, making it suitable for general-purpose regex needs. Hyperscan excels in scenarios requiring fast processing of multiple patterns simultaneously, particularly in network security and content inspection applications.

regular expression library

Pros of Oniguruma

  • Supports a wider range of regular expression syntax, including Perl, Python, and Emacs styles
  • Offers better support for Unicode and multi-byte character sets
  • Provides more advanced features like look-behind assertions and recursive patterns

Cons of Oniguruma

  • Generally slower performance compared to RE2, especially for complex patterns
  • Less memory-efficient, particularly for large-scale text processing tasks
  • Not as well-suited for high-throughput, production environments

Code Comparison

RE2:

RE2 pattern("\\w+");
re2::StringPiece input("Hello, World!");
std::string word;
while (RE2::FindAndConsume(&input, pattern, &word)) {
    std::cout << word << std::endl;
}

Oniguruma:

regex_t* reg;
OnigRegion* region;
onig_new(&reg, (UChar*)"\\w+", (UChar*)"\\w+"+4, ONIG_OPTION_DEFAULT, ONIG_ENCODING_UTF8, ONIG_SYNTAX_DEFAULT, NULL);
region = onig_region_new();
const char* str = "Hello, World!";
while (onig_search(reg, (UChar*)str, (UChar*)(str + strlen(str)), (UChar*)str, (UChar*)(str + strlen(str)), region, ONIG_OPTION_NONE) >= 0) {
    // Process matches
}

Both RE2 and Oniguruma are powerful regular expression libraries, but they cater to different use cases. RE2 focuses on speed and safety, making it ideal for high-performance applications. Oniguruma offers more extensive feature support and flexibility, suitable for complex pattern matching scenarios.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

This is the source code repository for RE2, a regular expression library.

For documentation about how to install and use RE2, visit https://github.com/google/re2/.

The short version is:

make make test make install make testinstall

Building RE2 requires Abseil (https://github.com/abseil/abseil-cpp) to be installed on your system. Building the testing for RE2 requires GoogleTest (https://github.com/google/googletest) and Benchmark (https://github.com/google/benchmark) to be installed as well.

There is a fair amount of documentation (including code snippets) in the re2.h header file.

More information can be found on the wiki: https://github.com/google/re2/wiki

Issue tracker: https://github.com/google/re2/issues

Mailing list: https://groups.google.com/group/re2-dev

Unless otherwise noted, the RE2 source files are distributed under the BSD-style license found in the LICENSE file.

RE2's native language is C++.

The Python wrapper is at https://github.com/google/re2/tree/main/python and on PyPI (https://pypi.org/project/google-re2/).

A C wrapper is at https://github.com/marcomaggi/cre2/. A D wrapper is at https://github.com/ShigekiKarita/re2d/ and on DUB (code.dlang.org). An Erlang wrapper is at https://github.com/dukesoferl/re2/ and on Hex (hex.pm). An Inferno wrapper is at https://github.com/powerman/inferno-re2/. A Node.js wrapper is at https://github.com/uhop/node-re2/ and on NPM (npmjs.com). An OCaml wrapper is at https://github.com/janestreet/re2/ and on OPAM (opam.ocaml.org). A Perl wrapper is at https://github.com/dgl/re-engine-RE2/ and on CPAN (cpan.org). An R wrapper is at https://github.com/girishji/re2/ and on CRAN (cran.r-project.org). A Ruby wrapper is at https://github.com/mudge/re2/ and on RubyGems (rubygems.org). A WebAssembly wrapper is at https://github.com/google/re2-wasm/ and on NPM (npmjs.com).