The most complete regular expression

Time:2020-1-8

A lot of friends who don’t know regular are often on the Internet for a long time when they need to use regular verification data, but the results are still not in line with the requirements. So I recently sorted out some regular expressions commonly used in development, including check numbers, characters, some special requirements and so on. Leave a background for yourself and make a reference for your friends.

1、 Check the expression of a number

Number: ^ [0-9]*$
N digits: ^ \ D {n}$
At least n digits: ^ \ D {n,}$
M-N digits: ^ \ D {m, n}$
Zero and non-zero digits: ^ (0| [1-9] [0-9] *)$
The number with two decimals at most at the beginning of non-zero: ^ ([1-9] [0-9] *) + (. [0-9] {1,2})$
Positive or negative numbers with 1-2 decimal places: ^ (\ -)? \ D + (\. \ D {1,2})$
Positive, negative, and decimal: ^ (\ – | \ +)? \ D + (\. \ D +)$
A positive real number with two decimal places: ^ [0-9] + (. [0-9] {2})$
A positive real number with 1-3 decimal places: ^ [0-9] + (. [0-9] {1,3})$
Nonzero positive integer: ^ [1-9] \ d * $or ^ ([1-9] [0-9] * {1,3} $or ^ \ +? [1-9] [0-9]*$
Nonzero negative integer: ^ \ – [1-9] [] 0-9 “* $or ^ – [1-9] \ D*$
Nonnegative integer: ^ \ D + $or ^ [1-9] \ d * | 0$
Non positive integer: ^-[1-9]d*|0$or ^ ((-d+) | (0+))$
Non negative floating point number: ^ \ D + (\. \ D +)? $or ^ [1-9] \ d * \. \ d * | 0 \. \ d * [1-9] \ d * | 0? \. 0 + | 0$
Non positive floating point number: ^ ((- \ D + (\. \ D +)) | (0 + (\. 0 +))) $or ^ (([1-9] \ d * \. \ d * | \ d * [1-9] \ d *) | \. 0 + |$
Positive floating point number: ^ [1-9] \ 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] *) |)$
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] *))$
Floating point number: ^ (-? \ D +) (\. \ D +)? $or ^ -? ([1-9] \ d * \. \ d * | 0 \. \ d * [1-9] \ d * | 0? \. 0 + | 0)$
2、 Expression for check character

Chinese characters: ^ [\ u4e00 – \ u9fa5] {0,}$
English and numbers: ^ [a-za-z0-9] + $or ^ [a-za-z0-9] {4,40}$
All characters 3-20 in length: ^. {3,20}$
A 26 letter string: ^ [a-za-z]+$
A string of 26 uppercase letters: ^ [A-Z]+$
A string of 26 lowercase letters: ^ [A-Z]+$
A string of numbers and 26 English letters: ^ [a-za-z0-9]+$
A string of numbers, 26 letters, or underscores: ^ \ W + $or ^ \ w {3,20}$
Chinese, English and numbers include underscores: ^ [\ u4e00 – \ u9fa5a-za-z0-9]+$
Chinese, English, numbers but excluding underscores and other symbols: ^ [\ u4e00 – \ u9fa5a-za-z0-9] + $or ^ [\ u4e00 – \ u9fa5a-za-z0-9] {2,20}$
You can enter characters such as ^% & ‘,; =? $\’: [^% & ‘,; =? $\ X22]+
Characters with ~ are not allowed: [^ ~ \ X22]+
3、 Expression of special requirements

Email address: ^ \ W + ([- +.] \ W +) * @ \ W + ([-.] \ W +) * \. \ W + ([-.] \ W +)*$
Domain name: [a-za-z0-9] [a-za-z0-9] {0,62} (/. [a-za-z0-9] [a-za-z0-9] {0,62}) + /?
Interneturl: [a-za-z] +: / / [^ \ S] * or ^ http: / / ([\ W -] + \) + [\ W -] + (/ [\ W -. /?% & =] *)$
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| 8| 9]) \ D {8}$
Phone numbers (“xxx-xxxxxxx”, “xxxx-xxxxxxx”, “xxx-xxxxxxx”, “XXXXXXX” and “XXXXXXXX”): ^ ($$\ D {3,4}-) | \ D {3.4}-)? \ D {7,8}$
Domestic phone number (0511-4405222, 021-87888822): \ D {3}- \ D {8}| \ D {4}- \ D {7}
ID card number (15 digits, 18 digits): ^ \ D {15}| \ D {18}$
Short ID number (number, end of letter X): ^ ([0-9]) {7,18} (x| x)? $or ^ \ D {8,18}| [0-9x] {8,18}| [0-9x] {8,18}$
Whether the account number is legal (starting with a letter, 5-16 bytes allowed, alphanumeric underline allowed): ^ [a-za-z] [a-za-z0-9 “{4,15}$
Password (starting with a letter, between 6 and 18, only letters, numbers and underscores): ^ [a-za-z] \ w {5,17}$
Strong password (must contain a combination of uppercase and lowercase letters and numbers. Special characters are not allowed. The length is between 8-10): ^ (? =. * [A-Z]) (? =. * [A-Z]). {8,10}$
Date format: ^ \ D {4} – \ D {1,2} – \ D {1,2}
12 months of a year (01-09 and 1-12): ^ (0? [1-9] | 1 [0-2])$
31 days of a month (01-09 and 1-31): ^ ((0? [1-9]) | ((1| 2) [0-9]) | (30| 31)$
Input format of money:

  • There are four forms of money we can accept: “10000.00” and “10000.00”, and “10000” and “10000” without “points”: ^ [1-9] [0-9]*$
  • This means any number that does not start with 0, but it also means that a character “0” does not pass, so we use the following form: ^ (0| [1-9] [0-9] *)$
  • A 0 or a number that does not start with 0. We can also allow a negative sign at the beginning: ^ (0| -? [1-9] [0-9] *)$
  • This means a zero or a number that may not start with a zero. Let the user start with a zero. Remove the negative sign, because money can’t be negative. What we want to add is the possible decimal part: ^ [0-9] + (. [0-9] +)$
  • It must be noted that there should be at least one digit after the decimal point, so “10.” is not acceptable, but “10” and “10.2” are acceptable: ^ [0-9] + (. [0-9] {2})$
  • In this way, we stipulate that there must be two decimal places after the decimal point. If you think it’s too harsh, you can do as follows: ^ [0-9] + (. [0-9] {1,2})$
  • This allows the user to write only one decimal place. Now it’s time to think about commas in numbers. We can do this: ^ [0-9] {1,3} (, [0-9] {3}) * (. [0-9] {1,2})$
  • 1 to 3 numbers, followed by any comma + 3 numbers, comma becomes optional instead of mandatory: ^ ([0-9] + | [0-9] {1,3} (, [0-9] {3}) *) (. [0-9] {1,2})$
  • Note: This is the final result. Don’t forget that “+” can be replaced by “*”. If you think empty strings are acceptable (strange, why?) finally, don’t forget to remove the backslash when using functions. The common mistakes are here

X M L file: ^ ([a-za-z] + -?) + [a-za-z0-9] + \ \. [x|x] [m|m] [l|l]$
Regular expression for Chinese characters: [\ u4e00 – \ u9fa5]
Double byte character: [^ \ X00 – \ XFF] (including Chinese characters, it can be used to calculate the length of string (one double byte character length meter 2, ASCII character meter 1))
Regular expression for blank lines: \ n \ s * \ R (can be used to delete blank lines)
Regular expressions of HTML Tags: < (\ s *?) [^ >] * >. *? < 1 > |) [^ >] * >. *? < 1 > | *? / >
Regular expression for leading and trailing whitespace characters: ^ \ s * | \ s * $or (^ \ s * | (\ s * $) (it can be used to delete leading and trailing whitespace characters (including spaces, tabs, page breaks, etc.), which is very useful expression)
Tencent QQ number: [1-9] [0-9] {4,} (Tencent QQ number starts from 10000)
China Post Code: [1-9] \ D {5} (?)! (China Post code is 6 digits)
IP address: \ D + \. \ D + \. \ D + \. \ D + (useful when extracting IP address)
IP address: ((?: (?: 25 [0-5] | 2 [0-4] \ \ d| [01] \ \ D? \ \) {3} (?: 25 [0-5] | 2 [0-4] \ \ d| [01]? \ \ D? \ \ d)) (provided by @ Fei Long San Shao, thanks for sharing)

The above is the real and most comprehensive regular expression. I hope it can help you learn and collect it quickly.