Home>

### The most complete collection of commonly used regular expressions-including check digits, characters, some special needs, etc

First, the expression of the check digit

1 number:^ [0-9] * \$

2 n-digit number:^ \ d {n} \$

3 Number with at least n digits:^ \ d {n,} \$

4 m-n digits:^ \ d {m, n} \$

5 Numbers starting with zero and non-zero:^ (0 | [1-9] [0-9] *) \$

6 Non-zero digits with up to two decimal places:^ ([1-9] [0-9] *) + (. [0-9] {1,2})?\$

7 Positive or negative numbers with 1-2 decimal places:^ (\-)?\ d + (\. \ D {1,2})?\$

8 Positive, negative, and decimal:^ (\-| \ +)?\ d + (\. \ D +)?\$

9 Positive real numbers with two decimal places:^ [0-9] + (. [0-9] {2})?\$

10 Positive real numbers with 1 to 3 decimal places:^ [0-9] + (. [0-9] {1,3})?\$

11 Nonzero positive integer:^ [1-9] \ d * \$or ^ ([1-9] [0-9] *) {1,3} \$or ^ \ +?[1-9 ] [0-9] * \$

12 Non-zero negative integers:^ \-[1-9] [] 0-9 "* \$or ^-[1-9] \ d * \$

13 Non-negative integer:^ \ d + \$or ^ [1-9] \ d * | 0 \$

14 Non-positive integers:^-[1-9] \ d * | 0 \$or ^ ((-\ 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 ^ (-([1-9] \ d * \. \ d * | 0 \. \ d * [1-9] \ d *)) | 0?\. 0+ | 0 \$

17 Positive floating point number:^ [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] *)) \$

18 Negative floating point number:^-([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) \$

Expressions for check characters

1 Chinese character:^ [\ u4e00- \ u9fa5] {0,} \$

2 English and numbers:^ [a-za-z0-9] + \$or ^ [a-za-z0-9] {4,40} \$

3 All characters of length 3-20:^. {3,20} \$

4 A string of 26 English letters:^ [a-za-z] + \$

5 A string of 26 uppercase English letters:^ [a-z] + \$

6 A string of 26 lowercase English letters:^ [a-z] + \$

7 A string of numbers and 26 English letters:^ [a-za-z0-9] + \$

8 A string of numbers, 26 English letters, or underscores:^ \ w + \$or ^ \ w {3,20} \$

9 Chinese, English, numbers include underscores:^ [\ u4e00- \ u9fa5a-za-z0-9 _] + \$

10 Chinese, English, numbers but not including underscores:^ [\ u4e00- \ u9fa5a-za-z0-9] + \$or ^ [\ u4e00- \ u9fa5a-za-z0-9] {2,20} \$

11 You can enter characters including ^%&",;=?\$\":[^%&",;=?\$\ x22] +

12 Prohibition of entering characters with ~:[^ ~ \ x22] +

Third, the special needs 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 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 Phone numbers ("xxx-xxxxxxx", "xxxx-xxxxxxxx", "xxx-xxxxxxx", "xxx-xxxxxxxx", "xxxxxxx", and "xxxxxxxx)":^ (\ (\ d {3,4}-) | \ d {3.4}-)?\ d {7,8} \$

6 Domestic phone numbers (0511-4405222, 021-87888822):\ d {3}-\ d {8} | \ d {4}-\ d {7}

7 ID number (15 digits, 18 digits):^ \ d {15} | \ d {18} \$

8 Short ID number (digits, letters ending with x):^ ([0-9]) {7,18} (x | x)?\$or ^ \ d {8,18} | [0-9x ] {8,18} | [0-9x] {8,18}?\$

9 Is the account legal (beginning with a letter,5-16 bytes are allowed, alphanumeric underscores are allowed):^ [a-za-z] [a-za-z0-9 _] {4,15} \$

10 password (starting with a letter,The length is between 6 and 18 and can only contain letters, numbers, and underscores):^ [a-za-z] \ w {5,17} \$

11 strong password (must contain a combination of uppercase and lowercase letters and numbers,You cannot use special characters,The length is between 8-10):^ (?=. * \ D) (?=. * [Az]) (?=. * [Az]). {8,10 } \$

12 Date format:^ \ d {4}-\ d {1,2}-\ d {1,2}

13 12 months of a year (01 ~ 09 and 1 ~ 12):^ (0?[1-9] | 1 [0-2]) \$

14 31 days of a month (01 ~ 09 and 1 ~ 31):^ ((0?[1-9]) | ((1 | 2) [0-9]) | 30 | 31) \$

15 Money input format:

16 1. There are four forms of money we can accept:"10000.00" and "10,000.00", and "10000" and "10,000" without "minutes":^ [1-9] [0-9] * \$

17 2.This means any number that does not start with 0,However, this also means that a character "0" does not pass, so we take the following form:^ (0 | [1-9] [0-9] *) \$

18 3.A 0 or a number that does not start with a 0.We can also allow a negative sign at the beginning:^ (0 |-?[1-9] [0-9] *) \$

19 4. This means a 0 or a number that may be negative and not start with 0. Let the user start with 0.Remove the negative sign,Because money can't always be negative.What we want to add below is a description of the possible decimal parts:^ [0-9] + (. [0-9] +)?\$

20 5.It must be stated thatThere should be at least 1 digit after the decimal point, so "10." is not passed,But "10" and "10.2" passed:^ [0-9] + (. [0-9] {2})?\$

21 6.In this way, we must have two digits after the decimal point.If you think it's too harsh,This can be:^ [0-9] + (. [0-9] {1,2})?\$

22 7.This allows users to write only one decimal place.Now we should consider the commas in the numbers,We can do this:^ [0-9] {1,3} (, [0-9] {3}) * (. [0-9] {1,2})?\$

23 8.1 to 3 numbers, followed by any number of commas + 3 numbers, commas become optional,Instead of having to:^ ([0-9] + | [0-9] {1,3} (, [0-9] {3}) *) (. [0-9] {1,2})?\$

24 Note:This is the end result,Don't forget that "+" can be replaced with "*" if you think empty strings are acceptable (odd, why?) Finally, don't forget to remove the backslash when using the functionGeneral errors are here

25 XML file:^ ([a-za-z] +-?) + [a-za-z0-9] + \\. [X | x] [m | m] [l | l] \$

26 Chinese character regular expression:[\ u4e00- \ u9fa5]

27 Double-byte characters:[^ \ 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 expression for blank lines:\ n \ s * \ r (can be used to delete blank lines)

29 html tagged regular expression:<(\ s *?) [^>] *>. *?</\ 1>|<&*. #?/> ;(The version circulating online is too bad.The above one can only be part,(It's still powerless for complex nested tags)

30 Regular expression of leading and trailing whitespace characters:^ \ s * | \ s * \$or (^ \ s *) | (\ s * \$) Breaks, form feeds, etc.), very useful expressions)

31 Tencent qq number:[1-9] [0-9] {4,} (tencent qq number starts from 10000)

32 China postal code:[1-9] \ d {5} (?! \ D) (China postal code is 6 digits) 33 IP address:\ d + \. \ D + \. \ D + \. \ d + (useful when extracting ip address) 34 ip address:((?:(?:25 [0-5] | 2 [0-4] \\ d | [01]?\ \ d?\\ d) \\.) {3} (?:25 [0-5] | 2 [0-4] \\ d | [01]?\\ d?\\ d))

### Related articles

• Previous Simple understanding of the use of exception handling in Java programming
• Next ThinkPHP form data intelligent write create method instance analysis