PHP Regex Tutorials and Examples

Php preg_match – test online

preg_match usage PHP regular expression match $retval = preg_match($pattern, $content [, array &$matches [, int $flags = 0 [, int $offset = 0]]]) (this page read more

Php preg_match_all – test online

preg_match_all usage PHP regular expression match $retval = preg_match_all($pattern, $content [, array &$matches [, int $flags = 0 [, int $offset = 0]]]) (this page read more

PHP preg_replace – test regex online

preg_replace usage PHP regular expression search replace usage $replaced_content = preg_replace($pattern, $replacement, $content [, int $limit = -1 [, int &$count]]) Notes on preg_replace behavior read more

PHP – Regex OR (alternation) examples using pipe

Regex OR (alternation) in php can be matched using pipe (|) match character. This is very frequently used in regular expressions. Following are some ways read more

PHP beginning and end of string regex examples

Php regex can use caret (^) for matching from beginning of string and dollar ($) for end of line. Here are some examples using preg_replace. read more

Php look ahead and look behind regex examples

Look ahead and look behind can be very useful to match a pattern which is followed, not followed, preceded or preceded by a certain pattern. read more

php preg_match – greedy and lazy regex examples

Php preg_match (preg_replace) can have greedy (default) or lazy regular expressions. Lazy match is done as soon as a valid match is found. Greedy match read more

PHP preg_replace – optional character group regex example – sandbox

PHP preg_replace example for optional char group match. Here the regular expression (regex) pattern contains optional character group enclosed within parentheses and followed by questiom read more

PHP preg_replace – optional one character regex example – sandbox

PHP preg_replace example for optional one char match. Here the regular expression (regex) pattern contains question mark (“?”) and the char precedded by “?” is read more

PHP – preg_replace back reference (in replacement text) examples

Sometimes we need to use back reference in PHP preg_replace to put back the matched pattern. One example can be if you want to append read more

PHP preg_replace – back reference within pattern example – sandbox

PHP preg_replace example for back reference within pattern. Here the match is grouped using parentheses and that group is accessed using backslash followed by the read more

PHP preg_replace – dot all (PCRE_DOTALL) example – sandbox

HP preg_replace example for dot all (PCRE_DOTALL) match. By default dot does not match newline char. When we specify dot all (using “s” regex modifier read more

Php regex delimiter examples

PHP requires regex patterns to have valid delimiters around it. Some languages may not require delimiters (e.g. python). To make code more readable, sometimes we read more

PHP regex – match any digit

Php regex can use digit shorthand (\d) to match a digit (0-9). To match any non digit character, regex shorthand \D (uppercase backslash D) can read more

PHP regex – whitespace shorthand (\s) regex examples

PHP regex whitespace shorthand \s can be used to match whitespace characters. These are the characters it will match. Char Dec val Oct val tab read more

PHP regex – word boundary examples

Php regex can use word boundary character (\b) to match a word boundary. Beginning of string if following by a word character will also match read more

PHP regex – match word character (letter, number or underscore)

Php regex can use word shorthand \w (lowercase backslash w) to match a word character. It matches with any of the following: letters (a-z,A-Z) Numbers read more

PHP remove non printable characters from a string

Regex to remove non printable characters from a string Space is first printable char and tilde (~) is last printable ascii char. We replace any read more