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
andmake
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
Built Distributions
Hashes for pcre2-0.1.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 502a70d1fe5a275a1a9613bb1eca4d0311b014a01c1ae7cf4565705b69484b65 |
|
MD5 | 8c6432defe06ef37848e2e51ae1ad78d |
|
BLAKE2b-256 | 58e6b208bb5106fb0b78829e8eedb5d9ff3f9d3f3231e953e72dc67da81f82e0 |
Hashes for pcre2-0.1.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0f43962e3de4a3d83ed0a503ef25e82e76126b71d86eb1caba2109a2757e665 |
|
MD5 | 6c75c560636f98876e4c43de152364d7 |
|
BLAKE2b-256 | 1307c7d290a97d1332745788a39bee79f4ab377be1d5118e653f7bad7d03d888 |
Hashes for pcre2-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecdd4f68de5ccc23f123e9c842fe32c79c67089d49b360b0feb3eedf07dbbafd |
|
MD5 | bf78f944a460f9915bde6b4ac993d093 |
|
BLAKE2b-256 | d1a37d246571df6a2d84e92d8a5a81b6273280d069358b9abd512773691f60a7 |
Hashes for pcre2-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3fc83b029ac0c2a0733f2f91be87e5c18351a7d2160c141b14499304d63a2d43 |
|
MD5 | 01478525cd6c901df16c8751fb14d9bf |
|
BLAKE2b-256 | 2a7ec28e5df8c84325aabd11023dd356cacfd0b67c284d121524f8999abc151c |
Hashes for pcre2-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94ea56b6bea05fb660403d786eb602bda28838ec563855dce465117baacbf3ff |
|
MD5 | 723a1923f2e055dff2e8ba23d996bb5e |
|
BLAKE2b-256 | 78dc46d748ba293b45869004365b6cc3757eba6fa4d1f20f7117cd3ad1d5501e |
Hashes for pcre2-0.1.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7a0ced94954a2849da4d9f91f4241a2f3f5f3386d0105879ea4859cd432b4ae |
|
MD5 | b783c69e9eaf7faea32db5f54dc055a8 |
|
BLAKE2b-256 | 62fad3b1b933869123638ec7ffdd7470c0a8f567a4fedbeabe6217fef79313ff |
Hashes for pcre2-0.1.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1177a3d5178471fd99fe76e32925d4b939742a319f017d52d3f0ce5a65dfbbac |
|
MD5 | d3a8b13d80f24812aa3c39c57318b4ba |
|
BLAKE2b-256 | 559b3f515ecd5c0b1e6b613adc67d2a550064cdb5754856966f375954f065d43 |
Hashes for pcre2-0.1.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82f644a2b10c8774296e9c8513b0ef14524f17bb03c3468fe0f3e9486590dce9 |
|
MD5 | da9a92b029afe4a284b17fb67a6b5f3b |
|
BLAKE2b-256 | 92435edbe6c226c9b36f67369c91de497d2653a5612f09dc5f785b197e4fd318 |
Hashes for pcre2-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28b3c17ab962c22116b0896dfb2a81f62dce2a5b6110178bc6d04416e771004a |
|
MD5 | e04233f767400e60dcb628442ebe3fdf |
|
BLAKE2b-256 | 517b235e9806c63162f3b47ffa01eb1f24f4ffddc248cbc7c7297a047cf8e824 |
Hashes for pcre2-0.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5503b96e557371397ae3086e6af99b4aa6449cd8840d58a14ca735525a5fe99e |
|
MD5 | e1b423e2188bd52d9d14915d1d009867 |
|
BLAKE2b-256 | 28f4ecde4b4556eaad2d109f8b17e4b2c07bf5f0831ebede7ae22df755a50772 |
Hashes for pcre2-0.1.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f341673a56ba339ed872d16e1267274243718809b38de7477534b1c66980a789 |
|
MD5 | a4c98913663a995e7aa2604206c6dfc2 |
|
BLAKE2b-256 | 951b1222a1b8e4c1b788bf4810a717a6d6f76c82693a5e5d3b615d86e0fba6b7 |
Hashes for pcre2-0.1.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | daee7c4ed3e998d8f18b5020ea53aaeca49082c23c62c023ea8f8e43268025c8 |
|
MD5 | 94d1e2a9b99b97fe8eab7d3965a5c810 |
|
BLAKE2b-256 | 46f9a7354055611199bd241fe8e499be50f90302143ac5faa509ce021c0f8e9c |
Hashes for pcre2-0.1.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 145c6c618159ea1c03a7ea78b9e59a2fbb8e645ebcfb6f648c238cf37b131b5d |
|
MD5 | cbb19f01f0506bcb23d87adf69d1a588 |
|
BLAKE2b-256 | 31c4e3db4c7d18c31343c5c846fa1fb0336ccd005d18b3601c19e1180e01465c |
Hashes for pcre2-0.1.0-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43ad2f8c7adf537d3f83d09ff7baa5a8b94595ba34570f1dd81e497c0ee40a60 |
|
MD5 | 716d6f2f2538188889bba2c31399d965 |
|
BLAKE2b-256 | cb62a55c61307e8bdabad1e327b392146e6f22c5ad155dd11baf4252c75e1c79 |
Hashes for pcre2-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e454e8ce3b0be6ba6ca2976d9dc83be7f7f3fc8a6af69833778cb032791a3c66 |
|
MD5 | e755519450c5ec73c8cd2889b55c617e |
|
BLAKE2b-256 | f0261d0f29b96597c57b122ab4ee085bc6ef07e564badf0e516b9a8264a2f4eb |
Hashes for pcre2-0.1.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cc03f7804585b693494e260f6d81d292aeef117003dcf3baedc8306aa10567f |
|
MD5 | 48842c001e4bcec781d13955c39c11ee |
|
BLAKE2b-256 | da36a0dc7074581f5b313805509b3141e27f335e6c030fed16af7f7effb7dbdc |
Hashes for pcre2-0.1.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff53c6cb8aaeb9230df7c2bbb84ce795ce9c79d3d8850714a2d53a4e4e7d20c4 |
|
MD5 | 7e712e6391c55b7bd6746c8e432a154c |
|
BLAKE2b-256 | 13910e8688a39525002706ca0db85e6cabb2e0355a49bef9c8a4164e3f3f17d7 |
Hashes for pcre2-0.1.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aee6846b72cc0dc894d1720c4d7096e5564da18c5f7d94a2169e0b541c69762a |
|
MD5 | 9a2cd46e53d4ad139593b01959cf4aba |
|
BLAKE2b-256 | 34648818c61f6f83fb5cb9de1b2a988d0df17becbd25b0e8068586cfb73aa199 |
Hashes for pcre2-0.1.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61ebc008be0e70c971c7d928963a1bbf5eb8a39b12e505d60dbe757fd49e26f0 |
|
MD5 | 41c1eab4a6bcf061e8818c73779db454 |
|
BLAKE2b-256 | 50a785fb2a9c108a41ef9e54b5e9107cda97f4446853909cd6981baa53e48365 |
Hashes for pcre2-0.1.0-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7481cba9e7a22825161984ba597a8ded9588652430bf86bb170034bf9d88451 |
|
MD5 | ba6b2d1317a2a28163b12150f1b38099 |
|
BLAKE2b-256 | 45f7b1a8438058569c58362bbeecdb46719053f73a72f92d3299c33c7ff7f77c |
Hashes for pcre2-0.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bcebc3a1d547bb76da2c1dabe6cc3acea382f2ce0ce51a0dfc66b451f2d611c0 |
|
MD5 | 66f1da4f605d7dbf39a4389c0d84becc |
|
BLAKE2b-256 | dd0d2d2e74e2b0eb48ee88280cb536a9407ef492f2caa3e2cc9a2ffc3e1b676e |
Hashes for pcre2-0.1.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 895b57a012e81e85f12bf334b68763acf9b19b12e8b8fd77e2ada33aafdfaf1a |
|
MD5 | 53f89b0793c32826f3180d3063f7fa6c |
|
BLAKE2b-256 | d7f1f3fba0b1a6a1767674f0f5c73722c6700dd863adac2dc0a70ca8b5d60d6d |
Hashes for pcre2-0.1.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc296a1cb9ef944765e4c6fb6da8b84ef48360ff0204cae625b863cf0161a9d2 |
|
MD5 | e76ec413cbbe0b929a30ce0b391ce69a |
|
BLAKE2b-256 | 071a2562641dee6a35248f8ec6b23235151fca0168fac072ec62f0c37c0e4f88 |
Hashes for pcre2-0.1.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f51ece8382fe0bf2d708fc71ec8381a9416d32889a726cf1822da456daf8c796 |
|
MD5 | 75ffc4ab3d2398a15da150f0d446e654 |
|
BLAKE2b-256 | 7906df6d9fd8999c0b267b3c1d409092ace44251342d011e016755b2d6def1c2 |
Hashes for pcre2-0.1.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21d1c6b896cbdee1f06d2a972dffd7b9d55602bd44343284d9a32de0128bf6ba |
|
MD5 | 9ef74e9f2e067dfd2c68f6f93df7ab19 |
|
BLAKE2b-256 | f46b5418ffe5ea49d102fea1dfb4549552f186311abd74458309f3f170da235e |
Hashes for pcre2-0.1.0-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6ac21907a3ac9088da700de5685e8a9eef3ff821c01b93c26cc80314eccb3f1 |
|
MD5 | 0154edb0f6a335bdcc361819533bc6b2 |
|
BLAKE2b-256 | b26c8fb0aa4c5f6a5ba34d2a74f6f0fe5691d520f170f4995548424f4422a691 |
Hashes for pcre2-0.1.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f62ad17ea7eab4504aa41283bdab60bc3f94b03bb3e9ab18b0b62c04043bd45 |
|
MD5 | 5d6269b9a7558aad2316b5793c73d5a4 |
|
BLAKE2b-256 | 729f1838a095d5076e118ed3dbcdce2ddedbe6beb77d00721f0a0e5d52261ea6 |
Hashes for pcre2-0.1.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2dd37a51be2d66f206360c012880f96d6abb096d983ce4b47087d4f606ed0ac9 |
|
MD5 | bb009a4ccb7bd2d4cf955c7270c777e1 |
|
BLAKE2b-256 | 8849cd92ce10015b20822f55a13ea860762559d6e5c0082db6139d8de09034d8 |
Hashes for pcre2-0.1.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90824505c1519ef0ed156477dfdf345428ae2af9d4b4aad7089497372f70c42b |
|
MD5 | b20e655786513ea34ee13a662adabc95 |
|
BLAKE2b-256 | 28d6cdacd57616aba8f8a49e186914c7fd7c53ddd33440f008fb3e04d399ab6a |
Hashes for pcre2-0.1.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb6741a7aec610768fa9f2895fb2b10f3c7b3a0929be4a0824d94c99b8e9ce6d |
|
MD5 | d897059ff88bfc9529da30f57e76c25e |
|
BLAKE2b-256 | 517b219b2c175c68118003f816d49269947a53f4e685fbe85a2735522d6bf4b7 |
Hashes for pcre2-0.1.0-cp36-cp36m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f834cb5fe4c1f6b3ae078433ba66fdb4ec96f6981b49da6336d42d2128e33527 |
|
MD5 | 5b6fd651b702c85da194b3df39c79e52 |
|
BLAKE2b-256 | 16fe7b078a9d73a76128f2122b7264925d47b69190eea344f78364cd24911dc1 |
Hashes for pcre2-0.1.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16d361792f73e86bae90afd5ae6bf180f1de9651276279f9bb6949bbd3e10167 |
|
MD5 | cf0c77b871f611606466bcd3a171ce80 |
|
BLAKE2b-256 | 4d370e9c7a1dbe6f61a0f8b4acb662274463356c8dd226bec8e0b5e56f095163 |
Hashes for pcre2-0.1.0-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 953955a7734222b5e378c37695ba8a05ffaa4c4325ce230f63db1d8bbb8cc9fd |
|
MD5 | 7f5678a51a522fcb4e10ede3515f8b61 |
|
BLAKE2b-256 | 1c9cf5f67382a63ec0812791ea42efa1149f00da8e97c6021c105a7f63b39e27 |
Hashes for pcre2-0.1.0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 574107b50061031e22229fab1c1f2ba9b20eb9cb2f421cf0aee5629eb7622715 |
|
MD5 | d9d408766e0e07c306ec414b87353303 |
|
BLAKE2b-256 | 64d0c9b4f98e75844755859e17c12fa6bfd83dc4bf63b68a72dc74acb0dbc528 |