PHP Regular Expression
In string match, one Regular Expression pattern can usually match multiple strings which is sometimes very useful.
Regular Expression functions include preg_match(), preg_replace(),
preg_split() etc.
<?PHP
$arr = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
foreach($arr as $dy)
{
if (preg_match("/t.?u/i",$dy))
echo "$dy\n";
}
Tuesday
Thursday
Saturday
?>
Regular Expression Syntax:
\
Escape special characters, e.g. \\ is "\", \+ is "+"
|
Alternation match. e.g. /(e|d)n/ matches "en" and "dn"
•
Any character, except \n or line terminator
[^ab]
Any character except a and b
[A-Z]
All uppercase A to Z letters
[a-z]
All lowercase a to z letters
[A-z]
All Uppercase and lowercase a to z letters
i{n}
i occurs n times in sequence
i{n1,n2}
i occurs n1 - n2 times in sequence
i{n,}
i occures >= n times
\r
Carriage return character
\xhhhh
Unicode with 4 characters of hexadecimal code hhhh
\xhh
Character with 2 characters of hexadecimal code hh
?=i
Lookahead matches only if i is followed
?!i
Lookahead matches only if i is not followed