As a first thought this post isn’t meant to be a super complete explanation of all the characters, quantifiers, modifiers of the regular expression world, neither it is a tutorial on how to use them.
This is just a cheat sheet of the, in my opinion, most used features of the complex world of regex.
PHP Regex Cheat Sheet
Base Chars | Meta Chars | ||
---|---|---|---|
\w |
Same as (a-z0-9_) | ^ |
Start of line/string |
\W |
Any non word characters | $ |
end of line/string |
\s |
Any whitespace chars | [ |
Start chars class |
\S |
Any non whitespace character | ] |
End char class |
\d |
All digits | | |
Or (x|y) x or y |
\D |
Non digit chars | ( |
Start subpattern |
\b |
Word boundary | ) |
End subpattern |
\B |
Not word boundary | \ |
Escape char |
. |
Any character |
Quantifiers | Modifiers | ||
---|---|---|---|
* |
Zero or more | i |
Ignore case |
+ |
One or more | m |
Multiline mode ^ and $ match start/end of line |
? |
Zero or one | s |
.* will include newline char |
{n} |
Exactly n | S |
Extra analysis of pattern (optimize reuse) |
{n,} |
At least n | U |
Ungreedy |
{,m} |
At most m | u |
Treat as UTF-8 |
{n,m} |
From n to m | more about modifiers |
For a much more detailed documentation visit: PHP: PCRE Patterns