Common regular expression finishing in PHP

Source: Internet
Author: User
Tags array contains explode expression mixed modifier regular expression
---------------------------------------------------------Regular Collection

Mobile Number:
$mode = "/^1[358]\d{9}/";

Email Address:
$mode = "/^[a-z][-_\.]? [A-z\d]*@[a-z0-9]+[\.] [A-z] {2,4}/i ";

----------------------------------------------------------Regular Foundation

$mode = "/^1[358]\d{9}/i";
The matching module must be in//start and end, and the second/post can be added to the mode modifier

Atomic
①a-z-A-Z _ 0-9//most common characters
② (ABC)//cell symbol enclosed in parentheses
③[ABCS] [^ABD]//atomic table enclosed in square brackets,
^ in atomic table excluding or opposing content

\d contains all numbers [0-9]
\d except for all numbers [^0-9]
\w contains all English characters [a-za-z_0-9]
\w except for all English characters [^a-za-z_0-9]
\s contains blank areas such as carriage return, line break, paging, etc. [\f\n\r]

Metacharacters
* 0 times 1 or more occurrences of the previous content
+ 1 or more times
? 0 times or 1 times
. Represents any one character (except carriage return line wrap)
Quite with the meaning of PHP ("or")
^ Force Match string Header content
$ force Match string tail content
[^ABC] matches content other than a or B or C
\b Match word boundaries, boundaries can be spaces or special symbols
\b Matches anything other than a word boundary
{m} matches the previous content with a repeat number of M times
{m,} repeat times greater than or m times to match previous content
{M,n} matches the repeat number of the previous content m times to N times
() overall match, and put into memory, you can use \\1 or \\2 ... Sequentially get

Priority: decreasing in descending order
() parentheses are highest because they are memory processed
* ? + {} Repeat match content second
^ $ \b Border Processing third
Conditional Treatment IV
Finally, the match is calculated in order of operation

Common modifiers: $mode = "/Regular/U";
I regular content is not case-sensitive when matching (default is distinguished)
M uses multiple line recognition matching when matching first content or tail content
S converts carriage return to space
x ignores whitespace in the regular
A force to start A match from scratch
D force $ match tail without any content \ n
U prohibits greedy matching, only tracks to the nearest match and ends,
Regular expressions commonly used in the acquisition program

Application
Preg_match_all (string pattern, string subject, array matches [, int flags])
Interception of more detailed content, collection of Web pages, analysis of text
Preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])
Preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])
Tip 1, the replacement content can be a regular or an array of regular
2, replacement content can be replaced by the modifier e to address the implementation of the content
Preg_split (string pattern, string subject [, int limit [, int flags]])
Through regular expressions to cut related content, similar to the previous learned explode cutting function, but explode
Can only be cut in one way with limitations.
-------------------------------------------------Debug Code
[Code]
<?php
$mode = "/^[a-z][-_\.]? [A-z\d]*@[a-z0-9]+[\.] [A-z] {2,4}/i ";
$str = "A12345@jb51.net";
echo $str. ' if (Preg_match ($mode, $str, $arr)) {
Echo ' Succeed-<font color=red> '. $arr [0];
}else{
Echo ' failed ';
}
?>
[Code]

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.