Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ numbers = pattern.findall(b"line 1\nline 22")
```

`pcre` mirrors the core helpers from Python’s standard library `re` module
`match`, `search`, `fullmatch`, `finditer`, `findall`, and `compile` while
`prefixmatch`, `match`, `search`, `fullmatch`, `finditer`, `findall`, and `compile` while
exposing PCRE2’s extended flag set through the Pythonic `Flag` enum
(`Flag.CASELESS`, `Flag.MULTILINE`, `Flag.UTF`, ...).

Expand All @@ -90,6 +90,9 @@ exposing PCRE2’s extended flag set through the Pythonic `Flag` enum
- Module-level helpers and the `Pattern` class follow the same call shapes as
the standard library `re` module, including `pos`, `endpos`, and `flags`
behaviour.
- Python 3.15's `prefixmatch()` alias is available at both the module level
and on compiled `Pattern` objects, and `re.NOFLAG` is re-exported as the
zero-value compatibility alias.
- `Pattern` mirrors `re.Pattern` attributes like `.pattern`, `.groupindex`,
and `.groups`, while `Match` objects surface the familiar `.re`, `.string`,
`.pos`, `.endpos`, `.lastindex`, `.lastgroup`, `.regs`, and `.expand()` API.
Expand Down
3 changes: 3 additions & 0 deletions pcre/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
fullmatch,
match,
parallel_map,
prefixmatch,
search,
split,
sub,
Expand Down Expand Up @@ -84,6 +85,7 @@ def escape(pattern: Any) -> Any:
# continue referencing familiar names. Prefer `pcre.Flag` for new code.
_FLAG_ZERO = Flag(0)
_FLAG_COMPAT_ALIASES = {
"NOFLAG": _FLAG_ZERO,
"IGNORECASE": Flag.CASELESS,
"I": Flag.CASELESS,
"MULTILINE": Flag.MULTILINE,
Expand Down Expand Up @@ -118,6 +120,7 @@ def escape(pattern: Any) -> Any:
"set_cache_limit",
"get_cache_limit",
"compile",
"prefixmatch",
"match",
"search",
"fullmatch",
Expand Down
7 changes: 6 additions & 1 deletion pcre/pcre.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ def match(
raw, resolved_end = _call_with_optional_end(self._pattern.match, subject, pos, endpos, options)
return self._wrap_match(raw, subject, pos, resolved_end)

prefixmatch = match

def search(
self,
subject: Any,
Expand Down Expand Up @@ -569,10 +571,13 @@ def compile(pattern: Any, flags: FlagInput = 0) -> Pattern:
return compiled


def match(pattern: Any, string: Any, flags: FlagInput = 0) -> Match | None:
def prefixmatch(pattern: Any, string: Any, flags: FlagInput = 0) -> Match | None:
return compile(pattern, flags=flags).match(string)


match = prefixmatch


def search(pattern: Any, string: Any, flags: FlagInput = 0) -> Match | None:
return compile(pattern, flags=flags).search(string)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "PyPcre"
version = "0.2.14"
version = "0.2.15"
description = "Modern, GIL-friendly, Fast Python bindings for PCRE2 with auto caching and JIT of compiled patterns."
readme = "README.md"
requires-python = ">=3.9"
Expand Down
15 changes: 15 additions & 0 deletions tests/test_api_parity.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ def test_purge_aliases_clear_cache():
assert pcre.purge is pcre.clear_cache


def test_prefixmatch_aliases_match():
assert pcre.match is pcre.prefixmatch
assert "prefixmatch" in pcre.__all__

module_match = pcre.prefixmatch(r"ab", "abcd")
assert module_match is not None
assert module_match.span() == re.match(r"ab", "abcd").span()

compiled = pcre.compile(r"ab")
pattern_match = compiled.prefixmatch("zab", pos=1)
assert pattern_match is not None
assert pattern_match.span() == re.compile(r"ab").match("zab", 1).span()


def test_error_aliases_and_escape():
assert pcre.error is pcre.PcreError
assert pcre.PatternError is pcre.PcreError
Expand All @@ -38,6 +52,7 @@ def test_error_aliases_and_escape():


def test_stdlib_style_flag_aliases():
assert pcre.NOFLAG == 0
assert pcre.IGNORECASE == pcre.Flag.CASELESS
assert pcre.I == pcre.Flag.CASELESS
assert pcre.MULTILINE == pcre.Flag.MULTILINE
Expand Down
Loading