Expand description
PHP’s PCRE functions: preg_match, preg_match_all, preg_replace,
preg_split and preg_quote, on PCRE2 10.44 with its JIT, the library
and version PHP 8.5 bundles.
| PHP | Rust |
|---|---|
preg_match($p, $s, $m, $flags, $offset) | preg_match |
preg_match($p, $s) | preg_match_bare |
preg_match_all($p, $s, $m, $flags, $offset) | preg_match_all |
preg_replace($p, $r, $s, $limit, $count) | preg_replace |
preg_split($p, $s, $limit, $flags) | preg_split |
preg_quote($s, $delimiter) | preg_quote |
preg_match($p, $s, $m) === 1 on a pattern compiled once | Regex::compiled, Regex::captures, Regex::is_match |
preg_last_error() / preg_last_error_msg() | PregError in every result |
User patterns must behave exactly as in PHP, down to where
pcre.backtrack_limit and the JIT stack run out, and only PCRE2 itself
does that: build.rs compiles the sources PHP 8.5 bundles
(ext/pcre/pcre2lib), and this module calls them as ext/pcre/php_pcre.c
does. Patterns are delimited and take modifiers exactly as
pcre_get_compiled_regex_cache() parses them (/i m n s x A D r S X U u J), then go through PHP’s per-process cache of compiled patterns, which
JIT-compiles them while pcre.jit is on (a JIT failure warns and turns it
off for the process). Matches run with pcre.backtrack_limit and
pcre.recursion_limit (their defaults) as the match and depth limits and
the 32 KiB–192 KiB JIT stack, through pcre2_jit_match() when no UTF-8
check is needed and pcre2_match() otherwise, and an empty match is
retried with PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED, as PHP does.
Structs§
- Argument
Error - A
ValueError/TypeErrorthrown for an invalid argument. - Array
- An ordered PHP array.
- Captures
- The groups of one match of
Regex::captures, aspreg_match()puts them in$matches. - Compile
Failure - A pattern that
pcre_get_compiled_regex_cache()rejected: the warning text, without the function name. - Match
Result - The result of
preg_matchandpreg_match_all: the return value (IntorBool(false)) and$matches(Nonewhen the pattern did not compile, which leaves$matchesuntouched). - Preg
- What a
preg_*call returned, withpreg_last_error()and the last warning it emitted ("preg_match(): Compilation failed: ..."). - Regex
- A compiled PHP regular expression (delimiters and modifiers included):
a
pcre_cache_entry. - Replace
Result - What
preg_replacereturns: the result (Str,ArrayorNull) and$count.
Enums§
- Key
- A PHP array key.
- Preg
Error preg_last_error().- StrOr
Array - A
string|arrayargument ofpreg_replace(). - Value
- The PHP values the
preg_*functions build.
Constants§
- PREG_
OFFSET_ CAPTURE - Returns each match with its byte offset, as PHP’s
PREG_OFFSET_CAPTURE. - PREG_
PATTERN_ ORDER - Orders
preg_match_all()results by group, as PHP’sPREG_PATTERN_ORDER. - PREG_
SET_ ORDER - Orders
preg_match_all()results by match, as PHP’sPREG_SET_ORDER. - PREG_
SPLIT_ DELIM_ CAPTURE - Returns the delimiter’s captured groups from
preg_split()too, as PHP’sPREG_SPLIT_DELIM_CAPTURE. - PREG_
SPLIT_ NO_ EMPTY - Leaves empty pieces out of
preg_split(), as PHP’sPREG_SPLIT_NO_EMPTY. - PREG_
SPLIT_ OFFSET_ CAPTURE - Returns each
preg_split()piece with its byte offset, as PHP’sPREG_SPLIT_OFFSET_CAPTURE. - PREG_
UNMATCHED_ AS_ NULL - Reports unmatched groups as
null, as PHP’sPREG_UNMATCHED_AS_NULL. - UNICODE_
VERSION - The Unicode version of PCRE2’s tables (PHP’s “PCRE Unicode Version”).
Functions§
- preg_
last_ error preg_last_error(): the error of the lastpreg_*call that set one.- preg_
match preg_match($pattern, $subject, $matches, $flags, $offset).- preg_
match_ all preg_match_all($pattern, $subject, $matches, $flags, $offset).- preg_
match_ bare preg_match($pattern, $subject):preg_match()without$matches. PHP then treats an empty match like the next match ofpreg_match_all(): it retries at the same position withPCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED, and an error there (a limit) makes the resultfalse. Passingflags:oroffset:by name givespreg_match()a$matchesto fill, so that ispreg_match.- preg_
quote preg_quote($str, $delimiter): only the first byte of the delimiter is used.- preg_
replace preg_replace($pattern, $replacement, $subject, $limit, $count).- preg_
split preg_split($pattern, $subject, $limit, $flags): an array, orBool(false).