Skip to main content

Module pcre

Module pcre 

Source
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.

PHPRust
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 onceRegex::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§

ArgumentError
A ValueError/TypeError thrown for an invalid argument.
Array
An ordered PHP array.
Captures
The groups of one match of Regex::captures, as preg_match() puts them in $matches.
CompileFailure
A pattern that pcre_get_compiled_regex_cache() rejected: the warning text, without the function name.
MatchResult
The result of preg_match and preg_match_all: the return value (Int or Bool(false)) and $matches (None when the pattern did not compile, which leaves $matches untouched).
Preg
What a preg_* call returned, with preg_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.
ReplaceResult
What preg_replace returns: the result (Str, Array or Null) and $count.

Enums§

Key
A PHP array key.
PregError
preg_last_error().
StrOrArray
A string|array argument of preg_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’s PREG_PATTERN_ORDER.
PREG_SET_ORDER
Orders preg_match_all() results by match, as PHP’s PREG_SET_ORDER.
PREG_SPLIT_DELIM_CAPTURE
Returns the delimiter’s captured groups from preg_split() too, as PHP’s PREG_SPLIT_DELIM_CAPTURE.
PREG_SPLIT_NO_EMPTY
Leaves empty pieces out of preg_split(), as PHP’s PREG_SPLIT_NO_EMPTY.
PREG_SPLIT_OFFSET_CAPTURE
Returns each preg_split() piece with its byte offset, as PHP’s PREG_SPLIT_OFFSET_CAPTURE.
PREG_UNMATCHED_AS_NULL
Reports unmatched groups as null, as PHP’s PREG_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 last preg_* 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 of preg_match_all(): it retries at the same position with PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED, and an error there (a limit) makes the result false. Passing flags: or offset: by name gives preg_match() a $matches to fill, so that is preg_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, or Bool(false).