Or how I learned to stop worrying and love the
PHLAK
@PHLAK
A regular expression, or "regex" for short,
is a set of literal and/or meta characters
that compose a search pattern
for matching strings.
A regular expression pattern must be enclosed by matching delimiters.
Delimiters can be any character that is NOT alphanumeric, a backslash or whitespace.
# ... #
@ ... @
Modifiers (or flags) may be added after the closing
delimiter
to change the functionality of the regular
expression.
| Modifier | Definition |
|---|---|
i |
Make the complete pattern case insensitive |
m |
Treat each line as individual lines |
https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
Match one or more literal charaters
| Pattern | Will Match | Won't Match |
|---|---|---|
| Symbol | Character |
|---|---|
\t |
Tab |
\r |
Carriage Return |
\n |
Line Feed |
\ ^ $ . [ ] | ( ) ? * + { }
Match any single character (except newline)
| Pattern | Will Match | Won't Match |
|---|---|---|
Match any character in a group or range
| Pattern | Will Match | Won't Match |
|---|---|---|
| |
||
Match none of the characters in a group or range
| Pattern | Will Match | Won't Match |
|---|---|---|
Matches the first complete pattern from left to right
| Pattern | Will Match | Won't Match |
|---|---|---|
Match the start or end of a string
| Pattern | Will Match | Won't Match |
|---|---|---|
Match a position between a word character and non-word character
| Pattern | Will Match | Won't Match |
|---|---|---|
| Symbol | Definition | Example |
|---|---|---|
\d |
Any decimal digit | |
\h |
Any horizontal whitespace character | |
\s |
Any whitespace character | |
\v |
Any vertical whitespace character | |
\w |
Any (ASCII) "word" character |
* Some additional non-printable characters omitted
Match zero or one of the preceding pattern
| Pattern | Will Match |
|---|---|
Match zero, one or more of the preceding pattern
| Pattern | Will Match | Wont Match |
|---|---|---|
Match specific quantities of the preceding pattern
| Pattern | Will Match | Wont Match |
|---|---|---|
By default quantifiers are greedy and will match as much text as they can.
Use ? to make a quantifier ungreedy (a.k.a. l.azy).
| Subject | Pattern | Result |
|---|---|---|
<strong>azPHP</strong> |
||
Groups allow you to apply quantifiers to a sub-pattern or to constrain alternation
| Pattern | Will Match |
|---|---|
Captures the results of a sub-pattern as a numbered group
| Pattern | Subject | Group | Content |
|---|---|---|---|
Arthur Dent |
0 | ||
| 1 | |||
| 2 |
Group a sub-pattern without capturing the result
| Pattern | Subject | Group | Content |
|---|---|---|---|
|
|
Ford Prefect
|
0 | |
| 1 |
Captures the results of a sub-pattern as a named group
| Pattern | Subject | Group | Content |
|---|---|---|---|
|
|
Zaphod Beeblebrox
|
0 | |
| first | |||
| last |
Use the result of a previous group as a pattern
| Pattern | Will Match | Wont Match |
|---|---|---|
|
|
Match a sub-pattern before/after the main expression without including it in the result.
| Pattern | Will Match | Wont Match | Result |
|---|---|---|---|
Arthur |
|||
Arthur |
|||
Prefect |
|||
Prefect |
The PCRE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl 5.
The PHP SPL includes several functions for working with regular expressions.
preg_filter preg_grep preg_last_error preg_match_all
preg_match preg_quote preg_replace_callback_array
preg_replace_callback preg_replace preg_split
preg_match( $pattern , $subject [, &$matches ] ): int
Searches $subject for a match to the regular
expression given in $pattern.
If $matches is provided, then it is filled with
the results of search.
Returns 1 if $pattern matchest
given $subject, 0 if it does not.
$subject = "Sirius Cybernetics Corporation";
preg_match('/[Cc]([a-z]+)/', $subject, $matches); // 1
// $matches = [ "Cybernetics", "ybernetics" ]
preg_match_all( $pattern , $subject [, &$matches ] ): int
Searches $subject for all matches to the
regular expression given in $pattern.
If $matches is provided, then it is filled with
the results of search.
Returns the number of full pattern matches (could be 0).
$subject = 'How much wood would a woodchuck chuck if a woodchuck could chuck wood?';
preg_match_all('/wood([a-z]+)/', $subject, $matches); // 2
// $matches = [
// [ "woodchuck", "woodchuck" ],
// [ "chuck", "chuck" ],
// ]
preg_replace( $pattern , $replacement, $subject ): mixed
Searches $subject for matches to $pattern
and replaces them with $replacement.
If matches are found, the new $subject will be returned,
otherwise $subject will be returned unchanged
preg_replace('/([Pp])ink/', '\1urple', 'John Pinkerton'); // John Purpleerton
preg_replace('/[^\d]/', null, '123-456-7890'); // 1234567890
preg_replace('#(\d{2})/(\d{2})/(\d{4})#', '\3-\1-\2', '05/20/1986'); // 1986-05-20
$pattern = ['/maroon/', '/gr[ae]y/', '/yellow|brown/'];
$replacement = ['red', 'silver', 'ugly'];
preg_replace($pattern, $replacement, 'The car was gray'); // The car was silver
preg_split( $pattern, $subject ): array
Split the given $subject by a string matched by $pattern.
preg_split('/[ .-]/', '1 123.456-7890'); // [ "1", "123", "456", "7890" ]
preg_quote( $str [, $delimiter ]): string
Escapes regular expression chracters from a string.
If $delimter is specified it will also be escaped.
Returns the escaped string.
preg_quote('[Foo]+Bar'); // \[Foo\]\+Bar
preg_quote('/path/to/file.txt', '/'); // \/path\/to\/file\.txt
https://www.regular-expressions.info
https://www.php.net/manual/en/pcre.pattern.php