Skip to content

Core\Regex::compile

Compiles a regular expression and returns a Core\Regex\Pattern that you can use many times.

Every Core\Regex method takes a pattern as a string or as a Pattern. Use compile when you need an option, or when you want to check a pattern at one place in your program. There are four options: caseInsensitive, multiline, dotAll and ungreedy. All four are false by default. A pattern has no delimiters, so you write ^\d+$ and not /^\d+$/i.

Good to know: compile checks the pattern at once. An invalid pattern throws a RuntimeError at that line, not later when you use it. A pattern cannot contain text from a request or another outside source. To search for such text, pass it through Core\Regex::quote first.

A pattern with lookaround or a backreference needs the backtracking engine. When nvs.toml sets [regex] backtracking = "deny", such a pattern written as a literal does not compile. Such a pattern built while the program runs throws a RuntimeError at compile.

Parameters

  • $patternstringThe regular expression, with no delimiters. A tainted string does not compile here. Use Core\Regex::quote to match a user's text literally.

Options

  • caseInsensitivebooloptionaltrue makes letters match in upper and lower case. The default is false.
  • multilinebooloptionaltrue makes ^ and $ match at the start and end of every line. The default is false.
  • dotAllbooloptionaltrue makes . match a newline too. The default is false.
  • ungreedybooloptionaltrue makes * and + match as little as possible, and *? as much as possible. The default is false.

Return value

Returns Core\Regex\Pattern. The compiled Core\Regex\Pattern. The pattern is checked here, so a mistake in it throws at this line and not at its first use.

Errors

  • RuntimeError$pattern is not a valid regular expression. The message contains the pattern.

Examples

Match upper and lower case

<?nvs
// Shows how to compile a pattern that matches letters in upper and lower case.
// `caseInsensitive: true` makes "yes", "Yes" and "YES" all match.
Core\Regex\Pattern $answer = Core\Regex::compile('^(yes|y)$', { caseInsensitive: true });
array<string> $replies = ['yes', 'Yes', 'YES', 'y', 'no', 'yes please'];
// This prints "agrees" for the first four replies only.
foreach ($replies as string $reply) {
if (Core\Regex::matches($reply, $answer)) {
echo $reply, ": agrees\n";
} else {
echo $reply, ": does not agree\n";
}
}
Output
yes: agrees
Yes: agrees
YES: agrees
y: agrees
no: does not agree
yes please: does not agree

A mistake in the pattern throws

<?nvs
// Shows what happens when a pattern has a mistake in it.
// This pattern opens a group with `(` and never closes it.
string $pattern = '(\d+-\d+';
// `compile` checks the pattern at once, so the error is thrown here.
// The message contains the pattern, so you can see which one is wrong.
try {
Core\Regex\Pattern $range = Core\Regex::compile($pattern);
echo "The pattern is valid.\n";
} catch (RuntimeError $e) {
echo "The pattern is not valid.\n";
echo 'The message names it: ', Core\Str::contains($e->message, $pattern) ? 'yes' : 'no', "\n";
}
Output
The pattern is not valid.
The message names it: yes

Find the error lines in a log

<?nvs
// Shows how to find every line of a log that starts with "ERROR".
string $log = "INFO server started\n"
. "ERROR disk is full\n"
. "INFO request done\n"
. "ERROR database timeout\n";
// `multiline: true` makes `^` and `$` match at the start and end of each line.
// Without it, `^` matches only at the start of the whole text.
Core\Regex\Pattern $errorLine = Core\Regex::compile('^ERROR (.+)$', { multiline: true });
// `matchAll` returns one match for each line. `group(1)` is the text after "ERROR ".
// This prints "disk is full" and "database timeout".
foreach (Core\Regex::matchAll($log, $errorLine) as Core\Regex\Match $found) {
echo $found->group(1), "\n";
}
Output
disk is full
database timeout