Learning notes for common regular expression matching in php

Source: Internet
Author: User
Tags lowercase ranges regular expression

Refer to the N + 1 Regular expression summary:

Common regular expression matching:

The code is as follows: Copy code
(11 | 12 | 13 | 14 | 15 | 21 | 22 | 23 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 41 | 42 | 43 | 44 | 45 | 46 | 50 | 51 | 52 | 53 | 54 | 61 | 62 | 63 | 64 | 65) [0-9] {4 }) ([1 | 2] [0-9] {3} [0 | 1] [0-9] [0-3] [0-9] [0-9] {3} [X0-9]) | ([0-9] {2} [0 | 1] [0-9] [0-3] [0-9] [0-9] {3 }))

Match Tencent QQ number: [1-9] [0-9] {4,} Note: Tencent QQ number starts from 10000
Price:/(^ [-+]? [1-9] d * (. d {1, 2 })? $) | (^ [-+]? [0] {1} (. d {1, 2 })? $ )/
Verify user password: "^ [a-zA-Z] w {5, 17} $" correct format: start with a letter, Length: 6 ~ It can only contain characters, numbers, and underscores.
China Postal code: [1-9] {1} (d +) {5}
China mobile phone number: (86) * 0 * 13d {9}

Mask:

The code is as follows: Copy code

/^ (254 | 252 | 248 | 240 | 224 | 192 | 128 | 0 ). 0.0.0 $ | ^ (255. (254 | 252 | 248 | 240 | 224 | 192 | 128 | 0 ). 0.0) $ | ^ (255.255. (254 | 252 | 248 | 240 | 224 | 192 | 128 | 0 ). 0) $ | ^ (255.255.255. (254 | 252 | 248 | 240 | 224 | 192 | 128 | 0 | 255) $/

Chinese phone number (including mobile phone and landline phone) :( (d {3, 4}) | d {3, 4}-| s )? D {7, 14}
Regular Expression "(d {3, 4}) | d {3, 4 }-)? D {0511} (-d {3}) * "(matching format: 4405222-87888822, 021-44055520, 021-555-0511, or (4405222)

China ID number:

The code is as follows: Copy code

(11 | 12 | 13 | 14 | 15 | 21 | 22 | 23 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 41 | 42 | 43 | 44 | 45 | 46 | 50 | 51 | 52 | 53 | 54 | 61 | 62 | 63 | 64 | 65) [0-9] {4 }) ([1 | 2] [0-9] {3} [0 | 1] [0-9] [0-3] [0-9] [0-9] {3} [X0-9]) | ([0-9] {2} [0 | 1] [0-9] [0-3] [0-9] [0-9] {3 }))

Matching space/blank line:

Regular expression for matching empty rows: n [s |] * r
Regular expression matching the first and last Spaces: (^ s *) | (s * $)
Regular Expression Matching Chinese characters: [u4e00-u9fa5]

Matching number:

Only numbers can be entered: "^ [0-9] * $"
Integer or decimal number: ^ [0-9] +. {} [0-9] {} $
You can only enter at least n digits: "^ d {n,} $ ".
Only m ~ can be input ~ N-digit :. "^ D {m, n} $"
Only numbers starting with zero and non-zero can be entered: "^ (0 | [1-9] [0-9] *) $"
Only positive numbers with two decimal places can be entered: "^ [0-9] + (. [0-9] {2 })? $"
Only 1 ~ Positive number of three decimal places: "^ [0-9] + (. [0-9] {1, 3 })? $"
Only a non-zero positive integer can be entered: "^ +? [1-9] [0-9] * $"
Only a non-zero negative integer can be entered: "^-[1-9] [] 0-9" * $
Only 3 characters can be entered: "^. {3} $"


Matching letter:
Only A string consisting of 26 English letters can be entered: "^ [A-Za-z] + $"
Only a string consisting of 26 uppercase letters can be entered: "^ [A-Z] + $"
You can only enter a string consisting of 26 lower-case English letters: "^ [a-z] + $"
Only strings consisting of digits and 26 English letters can be entered: "^ [A-Za-z0-9] + $"
Only a string consisting of digits, 26 English letters, or underscores can be entered: "^ w + $"
Check whether ^ % & ',; =? $ "And other characters:" [^ % & ',; =? $ X22] +"

The following is an example of your own regular expression application.

The code is as follows: Copy code
Function pregt( $ test ){
/**
Phone number matching
Telephone number rules:
Area Code: 3 to 5 digits, mostly four digits, Beijing (010) and Shanghai (021). Tibet has five digits, which can be wrapped in brackets or not.
If the area code is enclosed by parentheses, there can be 0 to 1 space between the area code and the number. If the area code is not enclosed by parentheses, there can be two lengths between the area code and the number, or-
Number: 7 to 8 digits
Example: (010) 12345678 or (010) 12345678 or 010 12345678 or 010--12345678
*/
$ Rule = '/^ (010) | (021) | (0d {3, 4 })))(?) ([0-9] {}) | (010 | 021 | 0d }) ([0-9] {7,8}) $/';
Preg_match ($ rule, $ test, $ result );
Return $ result;
 }

 

 

The code is as follows: Copy code
Function pregURL ($ test ){
/**
Matching url
Url rules:
Example
Protocol: // domain name (www/tieba/baike...). Name. Suffix/file path/file name
Http://www.111cn.net
Protocol: // domain name (www/tieba/baike...). Name. Suffix/file path/file name? Parameters
Www.www.111cn.net
Protocol: // domain name (www/tieba/baike...). Name. Suffix/file path/file name/parameter
Http://www.111cn.net
    
Protocol: dispensable, consisting of uppercase and lowercase letters. If the protocol is not written, it should not exist: //; otherwise, it must exist ://
Domain name: must exist, consisting of uppercase and lowercase letters
Name: it must exist. It must contain letters, numbers, and Chinese characters.
Suffix: must exist, consisting of uppercase and lowercase letters and.
File path: optional, consisting of uppercase and lowercase letters and numbers
File name: optional, consisting of uppercase and lowercase letters and numbers
Parameter: optional? Start? Parameters must start
*/
$ Rule = '/^ ([a-zA-Z] + )(://))? ([A-zA-Z] +). (w +). ([w.] +) (/([w] + )/?) * (/[A-zA-Z0-9] +. (w +) * (/([w] + )/?) *(? (W + =? [W] *) * (&? W + =? [W] *) * $ /';
Preg_match ($ rule, $ test, $ result );
Return $ result;
 }

 

The code is as follows: Copy code
Function pregPOS ($ test ){
/**
Matching zip code
Rule: six digits. The first digit cannot be 0.
*/
$ Rule = '/^ [1-9] d {5} $ /';
Preg_match ($ rule, $ test, $ result );
Return $ result;
 }

 

The code is as follows: Copy code
Function pregCh ($ test ){
// Match Chinese characters in utf8
$ Rule = '/([x {4e00}-x {9fa5}]) {1}/U ';
Preg_match_all ($ rule, $ test, $ result );
Return $ result;
 }
The code is as follows: Copy code

Function pregTI ($ test ){
/**
Matching time
Rules:
The format can be:
Year-month-day hour: minute: Second
Year-month-day hour: minute
Year-Month-Day
Year: four digits starting with 1 or 2
Month: the number of 1-9 digits; the number of two digits starting with 0 or 1. The number of digits starting with 0 is the number of one to nine digits, and the number of digits starting with 1 is the number of one to two digits.
Day: number of 1-9 digits; two digits starting with 0 or 1 or 2 or 3. The number of digits starting with 0 is one to nine digits, the number of digits starting with 1 or 2 is 0 to 9, and the number of digits starting with 3 is 0 or 1.
Hour: one digit between 0 and 9; two digits starting with 0 or 1; one digit ranges from 0 to 9; two digits starting with 2, and one digit ranges from 0 to 3.
Minute: one digit from 0 to 9; two digits starting with 0 to 5; one digit is from 0 to 9;
Minute: one digit from 0 to 9; two digits starting with 0 to 5.
*/
$ Rule = '/^ ([1-2] [0-9] {3}-) ([1-9]) | (0 [1-9]) | (1 [0-2])-) ([1-9]) | (0 [1-9]) | ([1-2] [0-9]) | (3 [0-1]) ([0-9]) | ([0-1] [0-9]) | (2 [0-3]) :( ([0-9]) | ([0-5] [0-9]) ([0-9]) | ([0-5] [0-9])? $ /';
Preg_match ($ rule, $ test, $ result );
Return $ result;
 }

 

The code is as follows: Copy code
Function pregIP ($ test ){
/**
Matching ip address
Rules:
** 1. ** 2. ** 3. ** 4
** 1 can be 1-9 of a single digit, 01-99 of two digits, 001-255 of three digits
** 2 and ** 3 can be 0-9 of a single digit, 00-99 of two digits, 000-255 of three digits
** 4 can be 1-9 of one digit, 01-99 of two digits, 001-255 of three digits
Four parameters must exist
*/
$ Rule = '/^ ([1-9]) | (0 [1-9]) | ([1-9] [0-9]) | (00 [1-9]) | (0 [1-9] [0-9]) | (1 [0-9] {2 }) | (2 [0-4] [0-9]) | (25 [0-5]).) ([0-9] {1, 2}) | ([0-1] [0-9] {2 }) | (2 [0-4] [0-9]) | (25 [0-5]).) {2} ([1-9]) | (0 [1-9]) | ([1-9] [0-9]) | (00 [1-9]) | (0 [1-9] [0-9]) | (1 [0-9] {2 }) | (2 [0-4] [0-9]) | (25 [0-5]) $ /';
Preg_match ($ rule, $ test, $ result );
Return $ 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.