Skip to main content

pyre2

Summary

pyre2 is a Python extension that wraps Google’s RE2 regular expression library. The RE2 engine compiles (strictly) regular expressions to deterministic finite automata, which guarantees linear-time behavior.

Intended as a drop-in replacement for re. Unicode is supported by encoding to UTF-8, and bytes strings are treated as UTF-8 when the UNICODE flag is given. For best performance, work with UTF-8 encoded bytes strings.

Installation

Normal usage for Linux/Mac/Windows:

$ pip install pyre2

Compiling from source

Requirements for building the C++ extension from the repo source:

  • Building requires RE2, pybind11, and cmake installed in the build environment.

    • On Ubuntu/Debian, install cmake, pybind11-dev, and libre2-dev packages

    • On Gentoo, install dev-util/cmake, dev-python/pybind11, and dev-libs/re2

    • For a venv you can install the pybind11 and cython packages from PyPI

On MacOS, use the brew package manager:

$ brew install -s re2 pybind11

On Windows use the vcpkg package manager:

$ vcpkg install re2:x64-windows pybind11:x64-windows

You can pass some cmake environment variables to alter the build type or pass a toolchain file (the latter is required on Windows) or specify the cmake generator. For example:

$ CMAKE_GENERATOR="Unix Makefiles" CMAKE_TOOLCHAIN_FILE=clang_toolchain.cmake tox -e deploy

Platform-agnostic building with conda

An alternative to the above is provided via the conda recipe (use the miniconda installer if you don’t have conda installed already).

Backwards Compatibility

The stated goal of this module is to be a drop-in replacement for re, i.e.:

try:
    import re2 as re
except ImportError:
    import re

That being said, there are features of the re module that this module may never have; these will be handled through fallback to the original re module``:

  • lookahead assertions (?!...)

  • backreferences (\\n in search pattern)

  • W and S not supported inside character classes

On the other hand, unicode character classes are supported (e.g., \p{Greek}). Syntax reference: https://github.com/google/re2/wiki/Syntax

However, there are times when you may want to be notified of a failover. The function set_fallback_notification determines the behavior in these cases:

try:
    import re2 as re
except ImportError:
    import re
else:
    re.set_fallback_notification(re.FALLBACK_WARNING)

set_fallback_notification takes three values: re.FALLBACK_QUIETLY (default), re.FALLBACK_WARNING (raise a warning), and re.FALLBACK_EXCEPTION (raise an exception).

Installation

Prerequisites:

  • The re2 library from Google

  • The Python development headers (e.g. sudo apt-get install python-dev)

  • A build environment with gcc or clang (e.g. sudo apt-get install build-essential)

  • Cython 0.20+ (pip install cython)

After the prerequisites are installed, install as follows (pip3 for python3):

$ pip install https://github.com/andreasvc/pyre2/archive/master.zip

For development, get the source:

$ git clone git://github.com/andreasvc/pyre2.git
$ cd pyre2
$ make install

(or make install3 for Python 3)

Documentation

Consult the docstring in the source code or interactively through ipython or pydoc re2 etc.

Unicode Support

Python bytes and unicode strings are fully supported, but note that RE2 works with UTF-8 encoded strings under the hood, which means that unicode strings need to be encoded and decoded back and forth. There are two important factors:

  • whether a unicode pattern and search string is used (will be encoded to UTF-8 internally)

  • the UNICODE flag: whether operators such as \w recognize Unicode characters.

To avoid the overhead of encoding and decoding to UTF-8, it is possible to pass UTF-8 encoded bytes strings directly but still treat them as unicode:

In [18]: re2.findall(u'\w'.encode('utf8'), u'Mötley Crüe'.encode('utf8'), flags=re2.UNICODE)
Out[18]: ['M', '\xc3\xb6', 't', 'l', 'e', 'y', 'C', 'r', '\xc3\xbc', 'e']
In [19]: re2.findall(u'\w'.encode('utf8'), u'Mötley Crüe'.encode('utf8'))
Out[19]: ['M', 't', 'l', 'e', 'y', 'C', 'r', 'e']

However, note that the indices in Match objects will refer to the bytes string. The indices of the match in the unicode string could be computed by decoding/encoding, but this is done automatically and more efficiently if you pass the unicode string:

>>> re2.search(u'ü'.encode('utf8'), u'Mötley Crüe'.encode('utf8'), flags=re2.UNICODE)
<re2.Match object; span=(10, 12), match='\xc3\xbc'>
>>> re2.search(u'ü', u'Mötley Crüe', flags=re2.UNICODE)
<re2.Match object; span=(9, 10), match=u'\xfc'>

Finally, if you want to match bytes without regard for Unicode characters, pass bytes strings and leave out the UNICODE flag (this will cause Latin 1 encoding to be used with RE2 under the hood):

>>> re2.findall(br'.', b'\x80\x81\x82')
['\x80', '\x81', '\x82']

Performance

Performance is of course the point of this module, so it better perform well. Regular expressions vary widely in complexity, and the salient feature of RE2 is that it behaves well asymptotically. This being said, for very simple substitutions, I’ve found that occasionally python’s regular re module is actually slightly faster. However, when the re module gets slow, it gets really slow, while this module buzzes along.

In the below example, I’m running the data against 8MB of text from the colossal Wikipedia XML file. I’m running them multiple times, being careful to use the timeit module. To see more details, please see the performance script.

Test

Description

# total runs

re time(s)

re2 time(s)

% re time

regex time(s)

% regex time

Findall URI|Email

Find list of ‘([a-zA-Z][a-zA-Z0-9]*)://([^ /]+)(/[^ ]*)?|([^ @]+)@([^ @]+)’

2

6.262

0.131

2.08%

5.119

2.55%

Replace WikiLinks

This test replaces links of the form [[Obama|Barack_Obama]] to Obama.

100

4.374

0.815

18.63%

1.176

69.33%

Remove WikiLinks

This test splits the data by the <page> tag.

100

4.153

0.225

5.43%

0.537

42.01%

Feel free to add more speed tests to the bottom of the script and send a pull request my way!

Current Status

The tests show the following differences with Python’s re module:

  • The $ operator in Python’s re matches twice if the string ends with \n. This can be simulated using \n?$, except when doing substitutions.

  • pyre2 and Python’s re may behave differently with nested groups.

    See tests/emptygroups.txt for the examples.

Please report any further issues with pyre2.

Tests

If you would like to help, one thing that would be very useful is writing comprehensive tests for this. It’s actually really easy:

  • Come up with regular expression problems using the regular python ‘re’ module.

  • Write a session in python traceback format Example.

  • Replace your import re with import re2 as re.

  • Save it as a .txt file in the tests directory. You can comment on it however you like and indent the code with 4 spaces.

Credits

This code builds on the following projects (in chronological order):

Metadata

Release files for pyre2 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyre2 0.3.3
File Size Uploaded
pyre2-0.3.3.tar.gz 28.7 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for pyre2 0.3.3
File
pyre2-0.3.3-cp39-cp39-win_amd64.whl CPython 3.9 CPython 3.9 Windows x86-64 Details
pyre2-0.3.3-cp39-cp39-manylinux2010_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.12+ x86-64 Details
pyre2-0.3.3-cp39-cp39-manylinux2010_i686.whl CPython 3.9 CPython 3.9 Linux glibc 2.12+ x86-32 Details
pyre2-0.3.3-cp39-cp39-macosx_10_9_x86_64.whl CPython 3.9 CPython 3.9 macOS 10.9+ x86-64 Details
pyre2-0.3.3-cp38-cp38-win_amd64.whl CPython 3.8 CPython 3.8 Windows x86-64 Details
pyre2-0.3.3-cp38-cp38-manylinux2010_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.12+ x86-64 Details
pyre2-0.3.3-cp38-cp38-manylinux2010_i686.whl CPython 3.8 CPython 3.8 Linux glibc 2.12+ x86-32 Details
pyre2-0.3.3-cp38-cp38-macosx_10_9_x86_64.whl CPython 3.8 CPython 3.8 macOS 10.9+ x86-64 Details
pyre2-0.3.3-cp37-cp37m-win_amd64.whl CPython 3.7 CPython 3.7 pymalloc Windows x86-64 Details
pyre2-0.3.3-cp37-cp37m-manylinux2010_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-64 Details
pyre2-0.3.3-cp37-cp37m-manylinux2010_i686.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-32 Details
pyre2-0.3.3-cp37-cp37m-macosx_10_9_x86_64.whl CPython 3.7 CPython 3.7 pymalloc macOS 10.9+ x86-64 Details
pyre2-0.3.3-cp36-cp36m-win_amd64.whl CPython 3.6 CPython 3.6 pymalloc Windows x86-64 Details
pyre2-0.3.3-cp36-cp36m-manylinux2010_x86_64.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-64 Details
pyre2-0.3.3-cp36-cp36m-manylinux2010_i686.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-32 Details
pyre2-0.3.3-cp36-cp36m-macosx_10_9_x86_64.whl CPython 3.6 CPython 3.6 pymalloc macOS 10.9+ x86-64 Details

Total release size: 12.4 MB

Release files / pyre2-0.3.3.tar.gz

Download URL pyre2-0.3.3.tar.gz
Size 28.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c6371a3494c33b74f4291aa755d6f50ebd78dc1d08e33e48f00c7517f60ef395
BLAKE2b-256 checksum
How to use checksums
0539063d346ffdf409165c5a6681fe0683bef37663c327cbf621a3ea354ab253
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp39-cp39-win_amd64.whl

Download URL pyre2-0.3.3-cp39-cp39-win_amd64.whl
Size 1.1 MB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
602aaad14849580213fde57cc04095aea8533fab2c7f88f7b75602636775933b
BLAKE2b-256 checksum
How to use checksums
d153d41dd3f0e46be60b3581c10b324df5b56fe9b5dbe3bd559500def892d1c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp39-cp39-manylinux2010_x86_64.whl

Download URL pyre2-0.3.3-cp39-cp39-manylinux2010_x86_64.whl
Size 909.4 kB
Tags CPython 3.9 Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
d065e838a71f57b2f72ee41adf17c0e80fcce6085a182f7644b10108e6cc091e
BLAKE2b-256 checksum
How to use checksums
11b31f19ad21aef2d14d6974c69496944a5459fff25078e11fb836e0b20a0d3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp39-cp39-manylinux2010_i686.whl

Download URL pyre2-0.3.3-cp39-cp39-manylinux2010_i686.whl
Size 886.0 kB
Tags CPython 3.9 Linux glibc 2.12+ x86-32
SHA-256 checksum
How to use checksums
761c0d8d14180e0fdc7df6f46072980a52b91642aaa4dca1e1be69dd9715ad8c
BLAKE2b-256 checksum
How to use checksums
5211f57d7eb0e38b9d4c0418aea5214492e16d05f905127d29035ae6a7e87bb5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp39-cp39-macosx_10_9_x86_64.whl

Download URL pyre2-0.3.3-cp39-cp39-macosx_10_9_x86_64.whl
Size 302.6 kB
Tags CPython 3.9 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
ce8ea0d3298a0597e1550b20a19c82742529cc872e0aa7d29f3ecc337abb3767
BLAKE2b-256 checksum
How to use checksums
94fc581ebb3e40951216b52d368181cc3102b5ba914ca8bd37f09f64ff0bf79c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp38-cp38-win_amd64.whl

Download URL pyre2-0.3.3-cp38-cp38-win_amd64.whl
Size 1.1 MB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
6cb377a847d1816f36f962529c0fbd31a3e683192d7417c5fa15374a6a4005e0
BLAKE2b-256 checksum
How to use checksums
a050de419e108cee323fe69d0aa882db4f2ff43e5ea82d75b4b79c11593f7cbc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp38-cp38-manylinux2010_x86_64.whl

Download URL pyre2-0.3.3-cp38-cp38-manylinux2010_x86_64.whl
Size 952.8 kB
Tags CPython 3.8 Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
22da50e72926b34069a2f7a27b2c5967652ffa2ab34bcb0b966a7d04ee03575c
BLAKE2b-256 checksum
How to use checksums
104dc532a3c5552f388d43cc39878afbbd2faef4335d59e55d3f48471a30a4c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp38-cp38-manylinux2010_i686.whl

Download URL pyre2-0.3.3-cp38-cp38-manylinux2010_i686.whl
Size 929.0 kB
Tags CPython 3.8 Linux glibc 2.12+ x86-32
SHA-256 checksum
How to use checksums
bf8c1aec2e1403fb4f1d7db4433f6e25fc5326f66697f6870238bfaa5211b2ae
BLAKE2b-256 checksum
How to use checksums
b5215a34bf5ef52eda44a11b6a5c44c706a19aeabe587a3a7f701b12b38fee74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp38-cp38-macosx_10_9_x86_64.whl

Download URL pyre2-0.3.3-cp38-cp38-macosx_10_9_x86_64.whl
Size 300.5 kB
Tags CPython 3.8 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
03ee5cec97dad38189991b26b23b9da983f837e0115056cb01889293f5a2ee23
BLAKE2b-256 checksum
How to use checksums
f86babc2f284f5e61ecd7ddd5436b197de2d75942cb9bbbef4759f1f1525b575
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp37-cp37m-win_amd64.whl

Download URL pyre2-0.3.3-cp37-cp37m-win_amd64.whl
Size 1.0 MB
Tags CPython 3.7 CPython 3.7 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
b136baea6dbc44bf1bdefd46ee3f0ca32e41f3a545fded3d3d2edc78c2ff840b
BLAKE2b-256 checksum
How to use checksums
83004af32430e27f34ef1ab94a6492b0c26326eab950c71dd7265f9184771ce7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp37-cp37m-manylinux2010_x86_64.whl

Download URL pyre2-0.3.3-cp37-cp37m-manylinux2010_x86_64.whl
Size 837.1 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
a95796586b91a857ee78a9c1b5098c2bdb6f547bc46d2b9af9fe9de840b9b953
BLAKE2b-256 checksum
How to use checksums
1d52fc9c0c58069510d06a08fc678e576925417bc85f1a6ecd1aef93d309b735
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp37-cp37m-manylinux2010_i686.whl

Download URL pyre2-0.3.3-cp37-cp37m-manylinux2010_i686.whl
Size 818.8 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-32
SHA-256 checksum
How to use checksums
47880f65203d005ed6cbb79641f4a2703d09bb769614e55ca8ea9b398ee0e847
BLAKE2b-256 checksum
How to use checksums
42e54df0d83c0edf3b245e866a2ea54f570f7c7d2a58e1a6586c7b265938f146
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp37-cp37m-macosx_10_9_x86_64.whl

Download URL pyre2-0.3.3-cp37-cp37m-macosx_10_9_x86_64.whl
Size 298.0 kB
Tags CPython 3.7 CPython 3.7 pymalloc macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
d6b11e2814f644ca91d2b8d2582f185c4dc7aa223a7b28c1a86d4770f4ca39e0
BLAKE2b-256 checksum
How to use checksums
56c1aa2e65514a8ddaeaabc9cf569e3d8c9f408ac7d428d999b3d563131a5841
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp36-cp36m-win_amd64.whl

Download URL pyre2-0.3.3-cp36-cp36m-win_amd64.whl
Size 1.0 MB
Tags CPython 3.6 CPython 3.6 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
d9a1f27145d766de312021af36951b71853a86acd21e7dd1e52358755e0bc259
BLAKE2b-256 checksum
How to use checksums
424153697c7fc3bdab64c0ff5d8fd26e67716c9f0cb577d0942592a265b6bf11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp36-cp36m-manylinux2010_x86_64.whl

Download URL pyre2-0.3.3-cp36-cp36m-manylinux2010_x86_64.whl
Size 844.6 kB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
190b99f6650e72a67085bd3e17b4660cb0eab1438436e033f1317ba8456cef95
BLAKE2b-256 checksum
How to use checksums
410060bd4785aa940d62cea98cad280612d3b7b2541bb8d233b1e579d7fc0588
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp36-cp36m-manylinux2010_i686.whl

Download URL pyre2-0.3.3-cp36-cp36m-manylinux2010_i686.whl
Size 820.6 kB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-32
SHA-256 checksum
How to use checksums
2f0766e348304ced08a8e2f3a64c822266588eeb8637f5bdd3056c503bc2c286
BLAKE2b-256 checksum
How to use checksums
7255c4ff1ab2a455bfc41eb54c2b6b5ddd25eb6d2d7e9c566b5e0045fe23c869
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / pyre2-0.3.3-cp36-cp36m-macosx_10_9_x86_64.whl

Download URL pyre2-0.3.3-cp36-cp36m-macosx_10_9_x86_64.whl
Size 303.4 kB
Tags CPython 3.6 CPython 3.6 pymalloc macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
39eb48f2cd613d87417ae9ea35e49598e51973d9c53e4d9dd64d11208bfc7c0a
BLAKE2b-256 checksum
How to use checksums
f9240df46426e8c02898e9c865812702bbf859843318385b7e9c1969d31d870c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release history Release notifications | RSS feed

0.3.5

16 release files

This release

0.3.3 This release

17 release files

0.3.2

10 release files

0.3.1

10 release files

0.3

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page