QUICK TIP - PHP Regex Cheat Sheet

0 1,234,835
1_php_for_beginners_become_a_php_master_cms_project.jpg | A&H Business Technology
Related posts

There are some key points that help PHP developers can manage their tasks well.

? – 0 or 1 occurrences
* – 0 or more occurrences
+ – 1 or more occurrences

{n} – n = number of occurrences
{n, m} – n = min occurrences, m = max occurrences
{n,} – At least n occurrences
{,m} – At most m occurrences

. – most characters (wildcard)
\w – English characters, numbers, and underscore “_”
\W (Uppercase W) – characters not matched by \w
\d – numeric digits [0-9]
\D – nondigits
\s – whitespace character (space, tab, etc)
\S – any non-whitespace character
\t – tab character
\n – newline character
[x] – character class – allowed characters
[^x] – character class – non-allowed characters
[\b] – inside character class: backspace character
\b – outside character class: word boundary

^ – start of line
$ – end of line
^$ – empty line
(x) – capture group
(?:x) – non-capture group

Pattern Modifiers – PHP

i – Case Insensitive
m – Multi line mode – ^ and $ match start and end of lines
s – Dotall – . class includes newline
x – Extended� comments and whitespace
e – preg_replace only � enables evaluation of replacement as PHP code
S – Extra analysis of pattern
U – Pattern is ungreedy
u – Pattern is treated as UTF-8

Assertions – PHP

(?=…) – Positive look ahead assertion foo(?=bar) matches foo when followed by bar
(?!…) – Negative look ahead assertion foo(?!bar) matches foo when not followed by bar
(?<=) – Positive look behind assertion (?<=foo)bar matches bar when preceded by foo
(?<!) Negative look behind assertion (?<!foo)bar matches bar when not preceded by foo
(?>) – Once-only sub-patterns (?>\d+)bar Performance enhancing when bar not present
(?(x)) – Conditional sub-patterns
(?(3)foo|fu)bar – Matches foo if 3rd sub-pattern has matched, fu if not
(?#) – Comment (?# Pattern does x y or z)

TinyURL for this post: https://tinyurl.com/y3lwuewx

Sorry, The Comment Form Is Temporarily Closed At This Time
You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Language:English