Skip to main content

pyre2: Python RE2 wrapper for linear-time regular expressions

Build CI Status Release CI Status GitHub tag (latest SemVer, including pre-release) https://badge.fury.io/py/pyre2.svg Conda CI Status License Python version version platforms downloads

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:

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

  • Build tools and libraries: RE2, pybind11, and cmake installed in the build environment.

    • On Ubuntu/Debian: sudo apt-get install build-essential cmake ninja-build python3-dev cython3 pybind11-dev libre2-dev

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

    • For a venv you can install the pybind11, cmake, 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

For development, get the source:

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

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).

Documentation

Consult the docstrings 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.

  • The pyre2 module and Python’s re may behave differently with nested groups. See tests/test_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 with as test_<name>.txt 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.5

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

Built distributions (wheels)

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

Download URL pyre2-0.3.5-cp39-cp39-win_amd64.whl
Size 1.1 MB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
5ae4c3353d0f860c6b1c23ab48694569c9f9474cccdd1792789825dee61d4c69
BLAKE2b-256 checksum
How to use checksums
efc6dc3a71f1215ed2dd07751d0e32109d944ef7a82045e4377b228c0bf00ef8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

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

Download URL pyre2-0.3.5-cp39-cp39-manylinux2010_x86_64.whl
Size 909.5 kB
Tags CPython 3.9 Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
602e281ec75c57c9b5be0913b61583d41d1f7b9d4035275da44d93d148552491
BLAKE2b-256 checksum
How to use checksums
e9c1df80fdf74bda35855b85b0520b79ea61b68f5ad314751b0bba26fb42ef5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

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

Download URL pyre2-0.3.5-cp39-cp39-manylinux2010_i686.whl
Size 887.0 kB
Tags CPython 3.9 Linux glibc 2.12+ x86-32
SHA-256 checksum
How to use checksums
e3c68dcf3a392c6ce0e54255822a62faeab937c6d6a619ca465be257ff976d29
BLAKE2b-256 checksum
How to use checksums
6b7c3a93a0c035fbd39cd6b4416056f237eb18f2e4b5cce02fa9d92b12055f6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

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

Download URL pyre2-0.3.5-cp39-cp39-macosx_10_9_x86_64.whl
Size 303.0 kB
Tags CPython 3.9 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
5ed0d173f7ef9b62e6bd8b940e27ad777bdc88d26d166801b928bd6790494ca9
BLAKE2b-256 checksum
How to use checksums
5ff939344c0aa3ee25849bbf6d4d602ea7822f8c402f741e77d36e807d1f7e7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

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

Download URL pyre2-0.3.5-cp38-cp38-win_amd64.whl
Size 1.1 MB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
cd6eb0932768c98d8e0cc9e23e9bdfabad5fd9700b1503a53bcc010683546163
BLAKE2b-256 checksum
How to use checksums
fee7b2b2f31b6070703ee587c586213ca3d6e219bd0cd993d5ca8f8351aac66e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

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

Download URL pyre2-0.3.5-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
63db854dd374ac767fb466f4d3795468301531e17e3a9b16e671e79a701d8962
BLAKE2b-256 checksum
How to use checksums
cb9cf20f3727699be4e10b91639f907a15ab8153d89a532e83dc6e524be677cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

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

Download URL pyre2-0.3.5-cp38-cp38-manylinux2010_i686.whl
Size 930.4 kB
Tags CPython 3.8 Linux glibc 2.12+ x86-32
SHA-256 checksum
How to use checksums
89d14e2350ad8380ae76c48beabf8e8e9a83ac46fe87825bfe4120052fe8a3df
BLAKE2b-256 checksum
How to use checksums
be34af4cd3904862deb203f5e6d9e923f94cdd9ad2f7c1dcc882baef77ee8cd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

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

Download URL pyre2-0.3.5-cp38-cp38-macosx_10_9_x86_64.whl
Size 300.7 kB
Tags CPython 3.8 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
fa18633c77fab2a998db980318f7ee5edf4aa5e3521ea7ebbb7ec304c5b461af
BLAKE2b-256 checksum
How to use checksums
3ad5fd2b9c975fbc7dff33caee6c7810d8a1ba66520eb4b2b19c3591c6957de3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

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

Download URL pyre2-0.3.5-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
aba06162a71f5361c15e6284b322992ebc7288194dec96a15cacf98e19643868
BLAKE2b-256 checksum
How to use checksums
81d40bbeaf0b43860d4fdc3a98b07c8018b23529972c0beb746419e1d228a50e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

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

Download URL pyre2-0.3.5-cp37-cp37m-manylinux2010_x86_64.whl
Size 836.5 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
81a1a74c57024e1e9a5410c761755c36ec289a3d155a012a621dc6b84a7d5695
BLAKE2b-256 checksum
How to use checksums
d31e0c4184fe5f355ab345ecf4a3cb014a56bd783eae24c2614d740beddf4c7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

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

Download URL pyre2-0.3.5-cp37-cp37m-manylinux2010_i686.whl
Size 817.6 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-32
SHA-256 checksum
How to use checksums
d6dc7bfe742a3c813c1946ecb3214d47ca8afe8f22daa03928beb8297664a5c9
BLAKE2b-256 checksum
How to use checksums
1d53e59483ee046e3b15f21948d64af1dfe8c6887a528b41bb84868ec40a3898
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

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

Download URL pyre2-0.3.5-cp37-cp37m-macosx_10_9_x86_64.whl
Size 298.1 kB
Tags CPython 3.7 CPython 3.7 pymalloc macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
7c808780225246ce2da190638148e447ac4d32cd8be4a45103bbc272680b853a
BLAKE2b-256 checksum
How to use checksums
65decc2af76349a50342843acc8fd150531c2d937d0b36dca7d4da52a926a3c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

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

Download URL pyre2-0.3.5-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
9b45ca4c6b07fe689af6b8694614a5d5689031308bea5f8133dc5a97afe72afb
BLAKE2b-256 checksum
How to use checksums
15f4a446eed4556b008e18a9d1838e2faafe80c62409d8cd08774bccdb7132ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

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

Download URL pyre2-0.3.5-cp36-cp36m-manylinux2010_x86_64.whl
Size 843.4 kB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
397ec83fad83ada531f51dd04c6a0c056aa9964c87e7da3c28bde389f32dcbea
BLAKE2b-256 checksum
How to use checksums
1cf35283d6cd74225f43d3c8d0dfc73070132ec61ea0154311a8365e0665dfbc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

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

Download URL pyre2-0.3.5-cp36-cp36m-manylinux2010_i686.whl
Size 819.5 kB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-32
SHA-256 checksum
How to use checksums
6875f128243413f30472c42e85df316cc774547b01c79fddbeac34c6958b3440
BLAKE2b-256 checksum
How to use checksums
79824a0f6cf0f5ae8edf23f6d7840282f92ee2dbf118dfcdb2d5c9d5b11db649
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

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

Download URL pyre2-0.3.5-cp36-cp36m-macosx_10_9_x86_64.whl
Size 297.8 kB
Tags CPython 3.6 CPython 3.6 pymalloc macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
b68b4c7e8ed4b39d94bae555cb4438d9e1835462f4ad1bfd9fa75d31b052b3c0
BLAKE2b-256 checksum
How to use checksums
9ad6f761590dc829a262d7eede9452892dfefea6dab5d4645e195c3ccf751c78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

Release history Release notifications | RSS feed

This release

0.3.5 This release

16 release files

0.3.3

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