Many friends who do not know much about regular check often look for regular check data on the Internet for a long time, but the results do not meet the requirements. So I recently sorted out some regular expressions commonly used in development and shared them here. Leave a background for yourself, and also give friends a reference. The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
1、 Check the expression of the number The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
1. Number: ^ [0-9]*$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
2. Number of n bits: ^ d {n}$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
3. At least n digits: ^ d {n,}$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
4. m-n digit: ^ d {m, n}$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
5. Numbers starting with zero and non-zero: ^ (0 | [1-9] [0-9] *)$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
6. Numbers with at most two decimal places starting with non-zero: ^ ([1-9] [0-9] *)+(. [0-9] {1,2})$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
7. Positive or negative numbers with 1-2 decimal places: ^ ( -)? \d+(\.\d{1,2})?$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
8. Positive number, negative number, and decimal: ^ ( - | +)? \d+(\.\d+)?$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
9. Positive real number with two decimal places: ^ [0-9]+(. [0-9] {2})$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
10. Positive real number with 1~3 decimal places: ^ [0-9]+(. [0-9] {1,3})$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
11. Non zero positive integer: ^ [1-9] d * $or ^ ([1-9] [0-9] *) {1,3} $or ^ +? [1-9][0-9]*$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
12. Non zero negative integer: ^ - [1-9] [] 0-9 "* $or ^ - [1-9] d*$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
13. Non negative integer: ^ d+$or ^ [1-9] d * | 0$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
14. Non positive integer: ^ - [1-9] d * | 0 $or ^ ((- d+) | (0+))$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
15. Non negative floating point number: ^ d+(. d+)$ Or ^ [1-9] d * \d*|0\. \d*[1-9]\d*|0?\. 0+|0$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
16. Non positive floating point number: ^ ((- d+(. d+)?)| (0+(\.0+)?))$ Or ^ (- ([1-9] d * . D * | 0 . D * [1-9] d *) | 0? 0+|0$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
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] *)$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
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] *)$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
19. Floating point number: ^ (-? D+) (. d+)$ Or ^ -? ([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
2、 Expression of check character The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
1. Chinese characters: ^ [ u4e00 -- u9fa5] {0,}$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
2. English and numbers: ^ [A-Za-z0-9]+$or ^ [A-Za-z0-9] {4,40}$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
3. All characters with a length of 3-20: ^ {3,20}$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
4. String composed of 26 English letters: ^ [A-Za-z]+$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
5. String composed of 26 uppercase English letters: ^ [A-Z]+$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
6. String composed of 26 lowercase English letters: ^ [a-z]+$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
7. String composed of numbers and 26 English letters: ^ [A-Za-z0-9]+$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
8. String composed of numbers, 26 English letters or underscores: ^ w+$or ^ w {3,20}$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
9. Chinese, English and numbers include underscores: ^ [ u4E00 -- u9FA5A-Za-z0-9_]+$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
10. Symbols in Chinese, English and figures, but excluding underscores: ^ [ u4E00 -- u9FA5A-Za-z0-9]+$or ^ [ u4E00 -- u9FA5A-Za-z0-9] {2,20}$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
11. You can enter ^%&',;=$\ "Equal characters: [^%&',;=? $ x22]+12 Characters containing~are prohibited: [^~ x22]+ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
12. Non printing special character matching: [ u0000 - u001F] The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
3、 Special demand expression The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
1. Email address: ^ w+([-+.] w+) * @ w+([-.] w+) * \w+([-.]\w+)*$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
2. Domain name: [a-zA-Z0-9] [- a-zA-Z0-9] {0,62} (/. [a-zA-Z0-9] [- a-zA-Z0-9] {0,62})+/? The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
3. Internet URL: [a-zA-z]+://[^ s] * or ^ http://([ w -]+.)+[ w -]+(/[ w -./?%&=] *)$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
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}$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
5. Telephone number ("XXX-XXXXXXX", "XXXX-XXXXXXXXX", "XXX-XXXXXXX", "XXX-XXXXXXXX", "XXXXXXXXX" and "XXXXXXXX"): ^ ( d {3,4}- )|\d {3.4}- )? \d{7,8}$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
6. Domestic telephone number (0511-4405222, 021-87888822): d {3}- \d{8}|\d {4}- \d{7} The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
7. ID card number (15 digits, 18 digits): ^ d {15} | d {18}$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
8. Short ID card number (ending with number and letter x): ^ ([0-9]) {7,18} (x | X)$ Or ^ d {8,18} | [0-9x] {8,18} | [0-9X] {8,18}$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
9. Whether the account number is legal (start with a letter, allow 5-16 bytes, allow alphanumeric underscores): ^ [a-zA-Z] [a-zA-Z0-9_] {4,15}$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
10. Password (starts with a letter, between 6 and 18 in length, and can only contain letters, numbers and underscores): ^ [a-zA-Z] w {5,17}$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
11. Strong password (must contain a combination of uppercase and lowercase letters and numbers, cannot use special characters, and the length is 8-10): ^ (?=. * d) (?=. * [a-z]) (?=. * [A-Z]) {8,10}$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
12. Date format: ^ d {4}- \d {1,2}- \d{1,2} The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
13. 12 months of a year (01~09 and 1~12): ^ (0? [1-9] | 1 [0-2])$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
14. 31 days of a month (01~09 and 1~31): ^ ((0? [1-9]) | ((1 | 2) [0-9]) | 30 | 31)$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
15. Input format of money: The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
16.1. We can accept four forms of money: "10000.00" and "10000.00", and "10000" and "10000" without "points": ^ [1-9] [0-9]*$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
17.2. This means any number that does not begin with 0, but it also means that a character "0" does not pass, so we use the following form: ^ (0 | [1-9] [0-9] *)$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
18.3. A 0 or a number that does not begin with 0. We can also allow a negative sign at the beginning: ^ (0 | -? [1-9] [0-9] *)$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
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 never be negative. What we want to add below is to explain the possible decimal part: ^ [0-9]+(. [0-9]+)$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
20. 5. It must be noted that there should be at least one digit after the decimal point, so "10." is not passed, but "10" and "10.2" are passed: ^ [0-9]+(. [0-9] {2})$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
21.6. In this way, we stipulate that there must be two digits after the decimal point. If you think it is too harsh, you can do this: ^ [0-9]+(. [0-9] {1,2})$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
22.7. This allows users to write only one decimal place. Now we should consider the comma in the number. We can do this: ^ [0-9] {1,3} (, [0-9] {3}) * (. [0-9] {1,2})$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
23 8.1 to 3 numbers, followed by any comma+3 numbers, the comma becomes optional instead of mandatory: ^ ([0-9]+| [0-9] {1,3} (, [0-9] {3}) *) (. [0-9] {1,2})$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
24. Note: This is the final result. Don't forget that "+" can be replaced by "*" if you think the empty string is acceptable (strange, why?) Finally, don't forget to remove the backslash when using the function. General errors are here The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
25. XML file: ^ ([a-zA-Z]+-?)+[a-zA-Z0-9]+ [x|X][m|M][l|L]$ The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
26. Regular expression of Chinese characters: [ u4e00 -- u9fa5] The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
27. Double byte characters: [^ x00 -- xff] (including Chinese characters, which can be used to calculate the length of a string (a double byte character length is 2, and an ASCII character length is 1)) The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
28. Regular expression of blank lines: n s * r (can be used to delete blank lines) The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
29. Regular expression of HTML tag:<( S *?) [^>] *>. *? |<. *?/> (The version circulated on the Internet is too bad. The above one is only partial, but still powerless for complex nested tags) The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
30. Regular expression of first and last blank characters: ^ s * | s * $or (^ s *) | ( s * $) The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
31. Tencent QQ number: [1-9] [0-9] {4,} (Tencent QQ number starts from 10000) The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
32. China Postal Code: [1-9] d {5} (?! d) (China Postal Code is 6 digits) The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
33. IP address: d+ \d+\. \d+\. \D+(useful for extracting IP addresses) The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html
34. IP address: (?: (?: 25 [0-5] | 2 [0-4] d | [01]? D? D) .) {3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)) The article originates from the fallen fish- https://www.duoluodeyu.com/2699.html