Introduction: when writing a program or webpage that processes strings, you will often find strings that comply with certain complex rules.
. Regular Expressions are syntaxes used to describe these rules.
For example, when determining the user's email address format, mobile phone number format, or collecting others' webpage content.
Php also often uses regular expressions. php has two common Regular Expression functions: preg_match and ereg.
I have just read preg_match today. The specific method is preg_match (mode, string subject, array matches );
Below is an example I wrote.
Copy codeThe Code is as follows:
<? Php
$ Mode = "/[^ 8 s]/"; // matching Module
$ Str = "sssjj88d"; // matched content
Echo "If (preg_match ($ mode, $ str, $ arr) {// matching function
Echo "matched". $ arr [0]; // $ arr [0]: match the first value of the result set.
}
Else {
Echo "matching failed ";
}
Result:
Regular expression (regular expression) "metacharacters ":
* Match the previous content zero or multiple times, that is, the previous content matches any one
. Match content 0 times 1 time or multiple times, but does not contain carriage return line breaks
+ Match the previous content once or multiple times (except null ).
| Select matching similar to in PHP | (because this operation conforms to the weak type, leading to the most overall match)
^ Match the first part of a string
$ Match string tail content
{A, B} indicates the number of times the previous content is matched. This indicates the number of times from a to B.
() Merge the overall match and put it into the memory. \ 1 \ 2… can be used... Obtain
Below is an example I wrote in php:
Copy codeThe Code is as follows:
<? Php
$ Mode = "/\ d {2, 4} (. *) \ d {1, 2} \ 1 \ d {1, 2}/"; // the easier the matching module writes, the better.
// $ Mode = "/2009 (. *) 9 \ 1 (10 )/";
$ Str = "2011/9/10 ";
If (preg_match ($ mode, $ str, $ arr )){
Echo "matched ". "<br/> <font color = red> ". $ arr [0]. "</font> <br/> <font size = '+ 4' color = blue> Happy Teachers' Day </font> ";
}
Else {
Echo "matching failed ";
}
?>
Result: