Skip to main content

Python bindings for the PCRE2 regular expression library

Project description

PCRE2.py: Python bindings for the PCRE2 regular expression library

This project contains Python bindings for PCRE2. PCRE2 is the revised API for the Perl-compatible regular expressions (PCRE) library created by Philip Hazel. For source code, see the official PCRE2 repository.

Installation

From PyPI:

pip install pcre2

If a wheel is not available for your platform, the source for PCRE2 is downloaded over HTTP from PCRE2 releases and built. Building requires:

  • autoconf
  • C compiler toolchain, such as gcc and make
  • libtool
  • Python headers

Usage

Regular expressions are compiled with pcre2.compile() which accepts both unicode strings and bytes-like objects. This returns a Pattern object. Expressions can be compiled with a number of options (combined with the bitwise-or operator) and can be JIT compiled,

>>> import pcre2
>>> expr = r'(?<head>\w+)\s+(?<tail>\w+)'
>>> patn = pcre2.compile(expr, options=pcre2.I, jit=True)
>>> patn.jit_compile()  # Patterns can also be JIT compiled after initialization.

Inspection of Pattern objects is done as follows,

>>> patn.jit_size
980
>>> patn.name_dict()
{1: 'head', 2: 'tail'}
>>> patn.options
524296

Once compiled, Pattern objects can be used to match against strings. Matching return a Match object, which has several functions to view results,

>>> subj = 'foo bar buzz bazz'
>>> match = patn.match(subj)
>>> match.substring()
'foo bar'
>>> match.start(), match.end()
(8, 17)

Substitution is also supported, both from Pattern and Match objects,

>>> repl = '$2 $1'
>>> patn.substitute(repl, subj)
'bar foo buzz bazz'
>>> patn.substitute(repl, subj, options=pcre2.G) # Global substitutions are also supported.
'bar foo bazz buzz'
>>> match.expand(repl)
'bar foo buzz bazz'

Additionally, Pattern objects support for scanning over subjects for all non-overlapping matches,

>>> for match in patn.scan(subj):
...     print(match.substring('head'))
...
foo
buzz

Performance

PCRE2 provides aa fast regular expression library, particularly with JIT compilation enabled. Below are the regex-redux benchmark results included in this repository,

Script Number of runs Total time Real time User time System time
vanilla.py 10 51.470 5.147 11.409 0.533
hand_optimized.py 10 12.310 1.231 2.484 0.212
pcre2_module.py 10 14.040 1.404 2.309 0.548

Tests were performed on an M2 Macbook Air. For more information on this benchmark, see The Computer Language Benchmarks Game. See source code of benchmark scripts for details and original sources.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pcre2-0.1.0.tar.gz (2.7 MB view hashes)

Uploaded Source

Built Distributions

pcre2-0.1.0-cp311-cp311-win_amd64.whl (2.0 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

pcre2-0.1.0-cp311-cp311-musllinux_1_1_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

pcre2-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pcre2-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.2 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

pcre2-0.1.0-cp311-cp311-macosx_11_0_arm64.whl (2.2 MB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

pcre2-0.1.0-cp311-cp311-macosx_10_9_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

pcre2-0.1.0-cp310-cp310-win_amd64.whl (2.0 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

pcre2-0.1.0-cp310-cp310-musllinux_1_1_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

pcre2-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pcre2-0.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.2 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

pcre2-0.1.0-cp310-cp310-macosx_11_0_arm64.whl (2.2 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

pcre2-0.1.0-cp310-cp310-macosx_10_9_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

pcre2-0.1.0-cp39-cp39-win_amd64.whl (2.0 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

pcre2-0.1.0-cp39-cp39-musllinux_1_1_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

pcre2-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pcre2-0.1.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.2 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

pcre2-0.1.0-cp39-cp39-macosx_11_0_arm64.whl (2.2 MB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

pcre2-0.1.0-cp39-cp39-macosx_10_9_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

pcre2-0.1.0-cp38-cp38-win_amd64.whl (2.0 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

pcre2-0.1.0-cp38-cp38-musllinux_1_1_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

pcre2-0.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pcre2-0.1.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.2 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

pcre2-0.1.0-cp38-cp38-macosx_11_0_arm64.whl (2.2 MB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

pcre2-0.1.0-cp38-cp38-macosx_10_9_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

pcre2-0.1.0-cp37-cp37m-win_amd64.whl (2.0 MB view hashes)

Uploaded CPython 3.7m Windows x86-64

pcre2-0.1.0-cp37-cp37m-musllinux_1_1_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

pcre2-0.1.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

pcre2-0.1.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.2 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

pcre2-0.1.0-cp37-cp37m-macosx_10_9_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

pcre2-0.1.0-cp36-cp36m-win_amd64.whl (2.0 MB view hashes)

Uploaded CPython 3.6m Windows x86-64

pcre2-0.1.0-cp36-cp36m-musllinux_1_1_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ x86-64

pcre2-0.1.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

pcre2-0.1.0-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.2 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ ARM64

pcre2-0.1.0-cp36-cp36m-macosx_10_9_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page