Php Regular expression (regar expression)

Source: Internet
Author: User
Tags php regular expression

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.