@ThomasWeinert
Find first match
preg_match($pattern, $subject);
preg_match($pattern, $subject, $matches);
preg_match($pattern, $subject, $matches, $flags, $offset);
Find all matches
preg_match_all($pattern, $subject);
preg_match_all($pattern, $subject, $matches);
preg_match_all($pattern, $subject, $matches, $flags, $offset);
Match the string "nevercodealone". This is case sensitive.
The modifier i allows case insensitive matches
Match the string "code". This is case insensitive.
Match the string cc.cc.cc.cc
. "c" can by any character except a newline.
What will be matched?
Define bytes/characters that will be matched.
The qualifier \d matches any digit (0-9
).
The qualifier \D matches anything except a digit.
Match the a string with the structure xxXxxXxxxx
. "x" represents a
digit, "X" a non digit.
Anchor your pattern to the start and/or end of the subject.
The ^ anchors the pattern to the string start.
Validate that the string starts with a digit.
The $ anchors the pattern to the string end.
Validate that the string ends with a digit.
The modifier D makes sure that a linefeed at the end of the subject is not ignored. Validate that the subject is a German zip code. It consists of 5 digits.
\A
- string start\Z
- string end, ignore linefeed\z
- string end, recognize linefeed\b
- word boundary[]
-
for ranges^
for negative matchesMatch all the vowels (aeiou) in the string.
Match all the non-vowels in the string.
Validate that the string consists of two characters. The characters can be digits or a letter between a and f.
How often will it be matched?
*
- any count?
- maximum of 1+
- minimum of 1{n}
- exactly n{n,m}
- minimum of n, maximum of m{n,}
- minimum of n{0,m}
- maximum of mThe {n}
syntax allows you to match a fixed repeat of qualifiers.
Validate that the subject is a German zip code. It consists of 5 digits.
The {n,m}
syntax allows you minimum and a maximum repetitions.
Validate that the subject is an 2 or 3 letter language code.
?
matches one or none. +
matches at least one repetition.
Validate an integer including an optional leading sign
The modifier u
activates Unicode UTF-8 mode.
\X
- extended unicode grapheme sequence\p{xx}
, \px
-
character with unicode property
\p{^xx}
, \P{^xx}
-
character without unicode property
\p{script}
- character from script\x{FFFF}
- code pointUse the unicode property L
to match any letter in the string
"English, Русский, 中文"
.
Match any Cyrillic letter in the subject.
(...)
- captured group(?<group_name>...)
- named group(?:...)
- group without capture((?i)...)
, (?i:...)
- group modifiersMatch a date in the format "YYYY-MM-DD". Capture each part into a named group (year, month, day).
Check if the the string contains 3 consecutive "ugh"s.
|
- alternative patternsMatch strings that start with a title ('Mr.', 'Ms.', 'Mrs.'), followed by a space and a string that contains at least one letter.
x
allows formatting#
- single line comment(?#...)
- comment group\Q...\E
- remove special meaning$pattern = '(/
(?:[a-zA-Z\\d_-]+\\.) #title
(?<mode>media|download|thumb)\\. # mode
(?:(?<preview>preview)\\.)? # is preview
(?<media_uri>
(?<id>[A-Fa-f\\d]{32}) #id
(?:v(?<version>\\d+))? #version
(?:\\.[a-zA-Z\\d]+)? #extension
)
$)Dix';
\1
, \g{1}
- reference group by index(?P=name)
, \g{name}
- reference group by name\g{-1}
- relative group referenceValidate strings that consist of the any count of same digit (11, 444, ...).
(?(DEFINE)(?<name>...))
(?&name)
Define a template that matches number between 0 and 255. Use the template to match an IP.
$pattern = '(^
(?:(?&number)\\.){3}(?&number)
(?(DEFINE)
(?<number>
25[0-5]| # 250 - 255
2[0-4]\\d| # 200 - 249
1?\\d{1,2} # 0 - 199
)
)
$)Dx';
(?=...)
, (?!...)
- Lookahead(?<=...)
, (?<!...)
- Lookbehind