-
Notifications
You must be signed in to change notification settings - Fork 1
Patterns and Regex
The regex rule matches a value against a regular
expression — either an inline body or the name of a pattern you registered.
Pass the regex body without delimiters. The value is matched anchored as
^(...)$, so the whole value must match.
use InitPHP\Validation\Validation;
$v = new Validation(['code' => 'AB12']);
$v->rule('code', 'regex([A-Z]{2}[0-9]{2})'); // passesCommas are argument separators. An inline body containing a comma — such as
[a-z]{2,4}— is split before it reaches the regex engine. Register it as a named pattern instead (below).
Give a pattern a name once and reference it by name. Names are matched
case-insensitively and persist across validation() runs.
$v->pattern('product_code', '[A-Z]{2}-[0-9]{4}');
$v->rule('sku', 'regex(product_code)');pattern(string $name, string $body) adds or overrides a named pattern. The
body has no delimiters and is matched as ^(...)$.
These names are available to regex out of the box:
| Name | Matches |
|---|---|
uri |
URI path characters |
slug |
URL slug characters (letters, digits, -, _) |
url |
URL characters |
alpha |
Unicode letters |
words |
Letters and whitespace |
alphanum |
Letters and digits |
int |
Digits |
float |
Digits, dot and comma |
tel |
Phone-number characters (0-9 + space ( ) -) |
text |
Common prose characters |
file |
A file name with an extension |
folder |
A folder name |
address |
Street-address characters |
date_dmy |
d-m-Y style dates |
date_ymd |
Y-m-d style dates |
email |
A basic e-mail shape |
$v->rule('handle', 'regex(slug)'); // "my-blog-post_2" passes
$v->rule('phone', 'regex(tel)');For common cases, prefer the purpose-built rule over a raw pattern — they are
clearer and handle edge cases (types, filter_var) for you:
| Instead of | Prefer |
|---|---|
regex(email) |
mail |
regex(int) |
integer |
regex(alphanum) |
alphanum |
Reach for regex when you need a shape the built-in rules do not express.
-
Rules Reference — the
regexrule entry. - Callable & Custom Rules — for logic regex cannot express.
initphp/validation · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Rules
Extending
Messages
Reference
Guides
Other