Pycregex, C-style POSIX.2 BRE syntax regex for Python
- Python 76.6%
- C 21.9%
- Makefile 1.5%
| src/pycregex | fix exception message | |
| tests | change match to search in API | |
| .gitignore | first commit | |
| COPYING | first commit | |
| Makefile | first commit | |
| MANIFEST.in | first commit | |
| pyproject.toml | fix package description in toml metadata | |
| README.md | fix readme | |
| setup.py | first commit | |
Pycregex, C-style POSIX BRE (and ERE) syntax regex for Python
This package brings libc's <regex.h> API to Python. While Python has re and there's the regex package, they do not have the syntax of <regex.h>. The simplest example is the regex pattern f\(oo\) which is not supported by either of the mentioned packages, because neither supports escaping the parentheses. The syntax of <regex.h> is specified in POSIX, see the Base Specification v8 Regular Expressions chapter.
This is not a glorious API, but if you need it, here it is.
Example
We search with subgroups, and show how the subgroup begin/end indices are accessed.
import pycregex
# Note that this package only works with binary strings. nmatch is
# number of groups plus one because we must include the entire match.
p = pycregex.compile(b"f\\(oo\\)", nmatch=2)
p.search(b"foo") # ==> 0
p.span() # ==> [(0, 3), (1, 3)]