Introduction: When you write a program or Web page that handles strings, you often find strings that match some complex rules
's needs. Regular expressions are the syntax used to describe these rules.
Example: In determining the user's email address format, mobile phone number format or collect other people's web content.
PHP is also used to regular expression PHP has two commonly used regular expression functions: Preg_match and Ereg.
I just saw preg_match today. Its specific wording is Preg_match (mode,string subject,array matches);
Here's a example I wrote.
Copy Code code as follows:
<?php
$mode = "/[^8s]/";//Match module
$str = "sssjj88d";//Match content
echo "if (Preg_match ($mode, $str, $arr)) {//Match function
echo "Match succeeded". $arr [0];//$arr [0]: matches the first value of the result set
}
else{
echo "Match failed";
}
Results:
Regular expression (regular expression) "meta character":
* matches the previous content 0 or more times, that is, the previous content any match
. Matching content 0 times 1 or more times, but does not include carriage return wrapping
+ 1 or more times (except empty) that match the previous content.
| Select match similar to PHP | (because this operation conforms to a weak type leading to the most complete match in the front)
^ Match String header content
$ Match String Tail content
{a,b}, which indicates the number of times the previous content was matched, from a to B.
() merges the whole match, and puts in the memory, can use \1 \2 ... Sequentially get
Here's a example I wrote in PHP:
Copy Code code as follows:
<?php
$mode = "/\d{2,4} (. *) \d{1,2}\\1\d{1,2}/";//the matching module is usually written as simple as possible.
$mode = "/2009 (. *) 9\\1 (10)/";
$str = "2011/9/10";
if (Preg_match ($mode, $str, $arr)) {
echo "Match succeeded". " <br/><font color=red> ". $arr [0]." </font><br/><br/><font size= ' +4 ' color=blue> teachers ' Day Happy </font>;
}
else{
echo "Match failed";
}
?>
Results: