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.
# ... #
@ ... @
Match one or more literal charaters
| Pattern | Will Match | Won't Match |
|---|---|---|
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 |
|---|---|---|
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 |
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