菜单

The most complete PHP regular expression in history (cell phone number needs to add 177-***)

2017年11月22日 - PHPer

First of all, look at regular expression mind maps:

A check digit expression

1 digit:

^[0-9]*$

The number of 2 n bits:

^d{n}$

3 at least n bits:

^d{n,}$

The number of 4 M-N bits:

^d{m, n}$

5 zero and non-zero start numbers:

^ (0|[1-9][0-9]*) $

6 non zero start numbers with two decimal numbers at most:

^ ([1-9][0-9]*) + (.[0-9]{1,2})? $

7 positive or negative numbers with 1-2 decimal places:

^ (-)? D+ (.D{1,2})? $?

8 positive numbers, negative numbers, and decimals:

^ (-|+)? D+ ($.D+)?

9 a positive real number with two decimal numbers:

^[0-9]+ (.[0-9]{2})? $?

10 positive real numbers with 1~3 decimal numbers:

^[0-9]+ (.[0-9]{1,3})? $?

11 nonzero positive integers:

^[1-9]d*$or {1,3}$or ^+ ^ ([1-9][0-9]*) [1-9][0-9]*$?

12 nonzero integers:

^-[1-9][]0-9 “*$or ^-[1-9]d*$

13 non negative integer:

^d+$or ^[1-9]d*|0$

14 non positive integers:

Or (^-[1-9]d*|0$^ (-d+) | (0+)).

15 non negative floating point numbers:

^d+ (.D+)? $or ^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$

16 non positive floating point numbers:

^ ((-d+ (.D+) | (?) 0+ (.0+)?) ^ (- $) or |0.0+|0$([1-9]d*.d*|0.d*[1-9]d*))?

17 positive floating point numbers:

Or (^[1-9]d*.d*|0.d*[1-9]d*$^ ([0-9]+.[0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*.[0-9]+) | ([0-9]*[1-9][0-9]*)).

18 negative floating point numbers:

^ ([1-9]d*.d*|0.d*[1-9]d*) or $^ (- (([0-9]+.[0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*.[0-9]+) | ([0-9]*[1-9][0-9]*))).

19 floating point numbers:

^ (-? D+) (.D+)? $^ – or? ([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0) $

Two, check the expression of characters

1 Chinese characters:

^[u4e00-u9fa5]{0,}$

2 English and numbers:

^[A-Za-z0-9]+$or ^[A-Za-z0-9]{4,40}$

3 all characters with length 3-20:

^.{3,20}$

4 a string consisting of 26 English letters:

^[A-Za-z]+$

5 a string consisting of 26 uppercase letters:

^[A-Z]+$

6 a string consisting of 26 lowercase letters:

^[a-z]+$

7 a string consisting of numbers and 26 English letters:

^[A-Za-z0-9]+$

8 a string consisting of numbers, 26 letters or underscores:

^w+$or ^w{3,20}$

9 Chinese, English, numbers including underscore:

^[u4E00-u9FA5A-Za-z0-9_]+$

10 symbols in Chinese, English and numbers, but not underlined:

^[u4E00-u9FA5A-Za-z0-9]+$or ^[u4E00-u9FA5A-Za-z0-9]{2,20}$

11 can input ^%&amp, ‘,, =, $, etc.’ characters:

[^%&?;? =? $x22]+

12 prohibit input of characters containing ~:

[^~x22]+

Three, the special needs of expression

1, Email address:

^w+ ([-+.]w+) *@w+ ([-.]w+) *.w+ ([-.]w+) *$

2, domain name:

[a-zA-Z0-9][-a-zA-Z0-9]{0,62} (/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62}) +/.?

3, InternetURL:

[a-zA-z]+: //[^s]* or ^http:// ([w-]+.) +[w-]+ (/[w-./?%& $=]*)?

4, mobile phone number:

^ (13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9]) d{8}$

5, telephone number (“XXX-XXXXXXX”, “XXXX-XXXXXXXX”, “XXX-XXXXXXX”, “XXX-XXXXXXXX”, “XXXXXXX” and “XXXXXXXX”):

^ ((d{3,4}-) |d{3.4}-)? D{7,8}$

6 domestic telephone numbers (0511-4405222, 021-87888822):

D{3}-d{8}|d{4}-d{7}

7, ID number:

15 or 18 ID card:

^d{15}|d{18}$

15 ID card:

^[1-9]d{7} ((0d) | (1[0-2]) (|3[0-1]) ([0|1|2]d)) d{3}$

18 ID card:

^[1-9]d{5}[1-9]d{3} ((0d) | (1[0-2]) (|3[0-1]) ([0|1|2]d)) d{4}$

8, short identity card number (number, letter X Ending):

^ ([0-9]) {7,18} (x|X)? $

or

^d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}? $?

9, whether the account is legal (the beginning of the letter, allowing 5-16 bytes, allowing alphanumeric underscore):

^[a-zA-Z][a-zA-Z0-9_]{4,15}$

10, the password (start with letters, length between 6~18, can only contain letters, numbers and underscores):

^[a-zA-Z]w{5,17}$

11, strong password (must contain the combination of uppercase letters and numbers, can not use special characters, length between 8-10):

^ (? =.*d) (? =.*[a-z]) (? =.*[A-Z]).{8,10}$

12, date format:

^d{4}-d{1,2}-d{1,2}

13, 12 months a year (01 to 09 and 1~12):

^ (0? [1-9]|1[0-2]) $

14, 31 days a month (01 ~ 09 and 1~31):

^ ((0? [1-9]) | ((1|2) [0-9]) |30|31).

15, the input format of money:

16 and 1., four forms of money can be accepted: “10000” and “10000”, and “no”, “10000” and “10000””:

^[1-9][0-9]*$

17, 2., which means any number that doesn’t start at 0, but it also means that a character “0” does not pass, so we use the following form:

^ (0|[1-9][0-9]*) $

18, 3. a 0 or less at the beginning of 0. We can also allow digital begins with a minus sign:

^ (0|-? [1-9][0-9]*) $

19, 4. this represents a 0 or a negative start is not 0 numbers. Let the user at the beginning of 0. The good minus is removed, because the money is not always negative. Here we want to add is that the fractional part of:

^[0-9]+ (.[0-9]+)? $?

20 and 5., it must be explained that there should be at least 1 digits behind the decimal point, so the “10.” is not passed, but “10” and “10.2” are passed:

^[0-9]+ (.[0-9]{2})? $?

21, 6., so we stipulate that there must be two bits at the decimal point, if you think it’s too harsh:

^[0-9]+ (.[0-9]{1,2})? $?

22 and 7. allow users to write only one decimal number. Here we should consider commas in numbers, and we can do that:

^[0-9]{1,3} (, [0-9]{3}) * (.[0-9]{1,2})? $

23, 8.1 to 3 numbers, followed by any comma, +3 numbers, comma becomes optional, not necessarily:

^ ([0-9]+|[0-9]{1,3} (([0-9]{3}) *) (.[0-9]{1,2})? $?

24: Note: This is the final result. Don’t forget that “+” can be replaced by “*”, and if you think the empty string is acceptable (strange, why?), finally, don’t forget to remove the backslash when you use the function, and the common mistakes are here

25, XML file:

^ ([a-zA-Z]+-?) +[a-zA-Z0-9]+\.[x|X][m|M][l|L]$

26. Regular expressions of Chinese characters:

[u4e00-u9fa5]

27, double byte character:

[^x00-xff]

(including Chinese characters) can be used to calculate the length of a string (a double byte character length meter 2, ASCII character meter 1)

28. Regular expressions for blank rows: ns*r (which can be used to delete blank rows)

29. Regular expressions labeled by HTML:

< (S*?) [^>]*> < /1> *? |< /> *? (online version of the spread is too bad, this is only part of the nested tag complex is still incapable of action)

Regular expressions, and 30 blank characters: ^s*|s*$or | (^s*) (s*$) (the end of the line can be used to remove whitespace characters (including spaces, tabs, formfeed etc.), expression is very useful)

31, Tencent QQ number: [1-9][0-9]{4,} (QQ numbers start from 10000)

32, China Postal Code: [1-9]d{5} (?? d) (China zip code is 6 digits)

33, IP address: d+.d+.d+.d+ (extract IP address useful)

发表评论

电子邮件地址不会被公开。