Regular Expression Tutorials and Examples

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 – 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 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 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 – 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 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 check if a filename is valid

Sometimes we need to check if a filename contains special character etc. One possible approach is to check for alphanumeric characters and few more characters read more