PHP Free Tutorial

Regular Expression Meta Characters in PHP

In PHP, Programming by Baqir Ali

Meta Characters in PHP

/love/ Find “love” anywhere in the string
/fun and games/ Finds “fun and games”
/(fun) and (games)/ Finds “fun and games”, “fun”, “games”. “fun” and “games” are sub-patterns within the original pattern
/(fun)/ First find fun as a pattern and then fun as a sub-pattern, fun would be search twice
/Love/i Case insensitive search
/love/A Must be at the start of the string
/love/D Must be at the end of the string
/5 pies/e When using preg_replace, if the replacement string has an expression, it would be first evaluated then used
/^B/ Words that start with B
/0$/ Words ending with 0
/\bbear\b/i Case insensitive search of the exact word “bear”
/a.b/ Starts with “a” then has any character (except newline) then “b”
/…/ Any string of three characters
/^… /”, Start of string has 3 characters
/J../ Starts with J and has two characters
/[abc]/ a, or b or c
/[a–z0–9_]/ a to z or 0 to 9
/[^a–z0–9_]/ Not a to z and not 0 to 9
/[0-9][0-9][0-9]/ 3 digits
/\d\d\d/ 3 digits
/\d{3}/ 3 digits
/^\w\w\w\W/ The start of the string has 3 alpha- numeric and one non-alpha-numeric
/\W\D/ A non-alpha-numeric and a non-digit
/e\s?[A-Z]/ Letter “e” followed by space or no- space (\s?) and a Capital alphabet. ? means 0 or 1
/B[a-z]*/ Uppercase B with 0 or more occurrences of small alphabets
/^[A-Z][a-z]*\s[A-Z][a-z]*\s/ The start of string is capital alphabet, then 0 or more small alphabets, then space, then a capital alphabet, then 0 or more small alphabets, then space
match(“/^[A-Z][a-z]*\s[A-Z][a- zAZ]*\ s/ The start of string is capital alphabet, then 0 or more small alphabets, then space, then a capital alphabet, then 0 or more capital or small alphabets, then space
/B[a-z]+/ Starting with capital B and then 1 or more small alphabet
/\s\d{3}$/ At the end of the string – One Space, 3 digits
/B.* / Starting with capital B, then 0 or more of any character, up until the last space
/B.*? / Starting with capital B, then 0 or more of any character, up until the first space
/Steve|Betty|Jon/ Steve or Betty or John
/(Steve|Alexander) Blenheim/ Steve Blengeim or Alexander Blenheim
/(ma)+/ Atleast one occurance of “ma” as a group
preg_replace(“$regex”,’$2, $1 $3′, $text); There are 3 sub-patterns in the regular expression, $1 represents the first one, $2 second and $3 third one
Social Security number (US) /^\d{3}-?\d\d-?\d{4}$/
U.S. Phone number /^\(?\d{3}\)?-?\s*\d{3}\s*-?\d{4}$/
Zip code /^\d{5}((-|\s)?\d{4})?$/
E—mail /^([0-9a-zA-Z]([-.\w]*[0-9a-zA- Z])*@([0-9a-zA-Z][-\w]*[0-9a-zAZ]\.)+[ a-zA-Z]{2,4})$/
Credit Card number /^((4\d{3})|(5[1-5]\d{2})|(6011))- ?\d{4}-?\d{4}- ?\d{4}|3[4,7]\d{13}$/
URL /^((http|https|ftp)://)?([\w- ])+(\.)(\w){2,4}([\w/+=%&_.~?- ]*)$/
MetacharacterWhat it matches?
. (single dot)Matches any character except a new line
[a-z0-9]Matches any character in a set
[^a-z0-9] Matches any character not in a set
\dMatches one digit
\DMatches non digit
\wMatches alphanumeric character
\WMatches non-alphanumeric character
\sMatches whitespace character
\SMatches non-whitespace character
\nMatches a new line
\rMatches a return
\tMatches a tab
\fMathes a form feed
\0Matches a null character
\bMatches a word boundry
\BMatches a nonword boundry
^Matches to the beginning of a line
$Matches end of the line
\AMatches beginning of the string only
x?Matches 0 or 1 occurrences of letter x
x*Matches 0 or more occurrences of letter x
x+Matches 1 or more occurrences of letter x
(xyz)+Matches one or more patterns of xyz
x{m,n}Matches at least m occurrences of the letterx and no more than n
was|were|willMathes one was, were or will
(?:x)Matches x but does not remember the match
x(?=y)Matches x if only followed by y
x(?!y) Matches x if only not followed by y

Support us by sharing this post