pyre2 Documentation

repository·main·Indexed 20 days ago

https://github.com/facebook/pyre2

A Python wrapper for Google's RE2 regular expression library providing a compatible interface to the built-in re module. It offers linear time complexity guarantees and specialized methods like test_search, test_match, and test_fullmatch for performance-sensitive tasks, though it lacks support for substitution, flags, and certain iteration methods.

Tokens
522
Snippets
1
Records
4
Agent score
21%

What's inside pyre2

  1. What is pyre2?

    main
    pyre2 is a Python extension that wraps Google's RE2 regular expression library. It provides a Python interface that implements many features of the built-in re module, making it a drop-in replacement for performance-sensitive regex tasks where RE2's guarantees (like linear time complexity) are required.
  2. Use pyre2 Regexp methods

    main

    pyre2 provides Regexp objects with several methods for matching.

    Key features include:

    • fullmatch: Works like match, but anchors the match at both the start and the end of the string.
    • test_search, test_match, and test_fullmatch: These methods return a boolean (True or False) indicating if a match was successful. They are generally faster than the standard search/match/fullmatch methods, particularly when using patterns with capturing groups, because they avoid the overhead of constructing match objects.
  3. Understand the limitations of pyre2

    main

    pyre2 is a subset of the standard re module. When using it, be aware of the following missing features:

    • No substitution methods: You cannot perform string replacements.
    • No flags: Regex flags (like re.IGNORECASE) are not supported.
    • No iteration/splitting: split, findall, and finditer are not implemented.
    • No top-level convenience functions: You cannot call pyre2.search() or pyre2.match() directly. You must first use compile() to create a Regexp object.
    • No compile cache: Performance-critical applications should manage their own pattern caching.
    • Match object limitations: Match objects do not support lastindex or lastgroup.
  4. Build pyre2 with RE2

    main

    Because RE2 does not build with fPIC by default, you must include specific CFLAGS when building the RE2 library itself:

    make CFLAGS='-fPIC -c -Wall -Wno-sign-compare -O3 -g -I.'

    When building the pyre2 Python module, you must provide the include and library paths for RE2 using CPPFLAGS and LDFLAGS:

    env CPPFLAGS='-I/path/to/re2' LDFLAGS='-L/path/to/re2/obj' ./setup.py build
    make CFLAGS='-fPIC -c -Wall -Wno-sign-compare -O3 -g -I.'
    
    env CPPFLAGS='-I/path/to/re2' LDFLAGS='-L/path/to/re2/obj' ./setup.py build