Sort common Regular Expressions in PHP

Source: Internet
Author: User

----------------------------------------------------------- Regular Expression collection

Mobile phone number:
$ Mode = "/^ 1 [358] \ d {9 }/";

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

------------------------------------------------------------ Regular Expression Basics

$ Mode = "/^ 1 [358] \ d {9}/I ";
The matching module must start and end with //. The second module can be followed by a pattern modifier.

Atom
① A-z A-Z _ 0-9 // The most common character
② (Abc) // The unit symbol enclosed in parentheses
③ [Abcs] [^ abd] // an atomic table enclosed in square brackets,
^ In the atomic table indicates exclusion or inverse content.

\ D contains all numbers [0-9]
\ D except all numbers [^ 0-9]
\ W contains all English characters [a-zA-Z_0-9]
\ W except all English characters [^ a-zA-Z_0-9]
\ S contains blank areas such as carriage return, line feed, and paging [\ f \ n \ r]

Metacharacters
* Match the previous content 0 times 1 time or multiple times
+ 1 or multiple times
? 0 or 1 time
. Represents any character (except for carriage return)
| Equivalent to php | ("or)
^ Force match the first part of the string
$ Force match string tail content
[^ Abc] matches content except a, B, or c.
\ B matches the word boundary. The boundary can be a space or a special symbol.
\ B matches the content except the word boundary
{M} matches the previous content with M duplicates.
{M,} matches the repeat of the previous content more than or equal to M
{M, n} matches the number of repetitions of the previous content M to N
() Overall match, and put it into memory, you can use \ 1 or \ 2... Obtain

Priority: Decrease in order
() Parentheses are the highest for memory processing.
*? + {} Duplicate Matching content followed
^ $ \ B boundary processing third
| Condition processing fourth
Finally, matching is calculated in the order of operation.

Common modifier: $ mode = "/regular/U ";
I. The regular content is case-insensitive during matching (the default value is case-sensitive)
M uses multiple lines to identify and match the first or last content
S. Convert the carriage return to a space.
X ignore the blank in the Regular Expression
A force match from scratch
D Force $ NO content at the end of the match \ n
U prohibits greedy matching. It only traces the latest matching character and ends,
Regular expressions commonly used in collection programs

Application
Preg_match_all (string pattern, string subject, array matches [, int flags])
Capture detailed content, collect webpages, and analyze 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 array regular
2. The replacement content can be replaced by the modifier e.
Preg_split (string pattern, string subject [, int limit [, int flags])
Regular expressions are used to cut related content. Similar to the explode cut function
Only one cutting method has 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.