A lot of people do not know regular friends, in the face of the need to use the regular calibration data, often on the internet to find a long time, the results are still not very satisfied with the requirements. So I've recently sorted out some of the regular expressions commonly used in development, including verifying numbers, characters, special needs, and so on. Leave a bottom for yourself, also give friends a reference.
One, the expression of the checksum number
Number: ^[0-9]*$
N-bit number: ^\d{n}$
at least n digits: ^\d{n,}$
m-n digits: ^\d{m,n}$
0 and non-0 digits: ^ (0|[ 1-9][0-9]*) $
A number with a maximum of two decimal digits beginning with 0: ^ ([1-9][0-9]*) + (. [ 0-9]{1,2})? $
A positive or negative number with 1-2 decimal places: ^ (\-)? \d+ (\.\d{1,2})? $
Positive, negative, and decimal: ^ (\-|\+)? \d+ (\.\d+)? $
Positive real number with two decimal places: ^[0-9]+ (. [ 0-9]{2})? $
Positive real number with 1~3 decimal: ^[0-9]+ (. [ 0-9]{1,3})? $
Non-zero positive integer: ^[1-9]\d*$ or ^ ([1-9][0-9]*) {1,3}$ or ^\+?[ 1-9][0-9]*$
Non-zero 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+) $
nonnegative 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*|0\.\d*[1-9]\d*)) |0?\.0+|0$
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]*)] $
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) $
Two, an expression for a checksum character
Kanji: ^[\u4e00-\u9fa5]{0,}$
English and digital: ^[a-za-z0-9]+$ or ^[a-za-z0-9]{4,40}$
All characters with length 3-20: ^.{ 3,20}$
A string of 26 English letters: ^[a-za-z]+$
A string of 26 uppercase letters: ^[a-z]+$
A string of 26 lowercase English letters: ^[a-z]+$
A string of numbers and 26 English letters: ^[a-za-z0-9]+$
A string consisting of numbers, 26 English letters, or underscores: ^\w+$ or ^\w{3,20}$
Chinese, English, and numbers include underscores: ^[\u4e00-\ u9fa5a-za-z0-9_]+$
Chinese, English, numeric but excluding underscore symbols: ^[\u4e00-\u9fa5a-za-z0-9]+$ or ^[\u4e00-\u9fa5a-za-z0-9]{2,20}$
Can enter ^%& ',; =?$\ ' and other characters:[^%& ', =?$\x22]+
prohibit entering characters containing ~: [^~\x22]+
III, special demand expressions
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|7|8| 9]) \d{8}$
Phone number ("Xxx-xxxxxxx", "xxxx-xxxxxxxx", "xxx-xxxxxxx", "xxx-xxxxxxxx", "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}
Identification number (15-digit, 18-digit): ^\d{15}|\ d{18}$
Short ID number (end of number, letter X): ^ ([0-9]) {7,18} (x| X)? $ or ^\d{8,18}| [0-9x] {8,18}| [0-9x] {8,18}?$
Account number is legal (start letter, allow 5-16 bytes, allow alphanumeric underscore): ^[a-za-z][a-za-z0-9_]{4,15}$
Password (beginning with a letter, length between 6~18, can only contain letters, numbers, and underscores): ^[a-za-z]\w{5,17}$
Strong password (must contain a combination of uppercase and lowercase letters and numbers, cannot use special characters, length between 8-10): ^ (? =.*\d) (? =.*[a-z]) (? =.*[a-z]). { 8,10}$
Date format: ^\d{4}-\d{1,2}-\d{1,2}
12 months of the year (01~09 and 1~12): ^ (0?[ 1-9]|1[0-2]) $
31 days of one months (01~09 and 1~31): ^ (0?[ 1-9]) | ((1|2) [0-9]) |30|31) $
input format for money:
- There are four kinds of representations of money we can accept: "10000.00″ and" 10,000.00″, and No "10000″" and "10,000″:^[1-9][0-9]*$
- " span> this represents any number that does not start with 0, but it also means that a character "0″ does not pass, so we take the following form: ^ (0|[ 1-9][0-9]*) $
- a 0 or a number that does not begin with 0. We can also allow a minus sign at the beginning: ^ (0|-?[ 1-9][0-9]*) $
- This represents a number that is 0 or one that may be negative at the beginning of 0. Let the user start with 0. The minus is also removed, because the money can not be negative. What we're going to add here is a description of possible decimal parts: ^[0-9 ]+(. [0-9]+)? $
- must indicate that there should be at least 1 digits after the decimal point, so "10." is not passed, but "10″ and" 10.2″ is passed: ^[0-9]+ (. [ 0-9]{2})? $
- So we have to specify two digits after the decimal point, and if you think it's too harsh, you can do this: ^[0-9]+ (. [ 0-9]{1,2})? $
- allows the user to write only one decimal. Let's consider the comma in the number below, we can do this: ^[0-9]{1,3} (, [0-9]{3}) * (. [ 0-9]{1,2})? $
- 1 to 3 digits followed by any comma + 3 digits, the comma becomes optional, not mandatory: ^ ([0-9]+|[ 0-9]{1,3} (, [0-9]{3}) *) (. [ 0-9]{1,2})? $
- Note: This is the end result, don't forget that "+" can be replaced with "*". If you think an empty string is acceptable (strange, why?) Finally, don't forget to remove the backslash when you use the function, the general error is here
XML file: ^ ([a-za-z]+-?) +[a-za-z0-9]+\\. [x| x][m| m][l| l]$
Regular expression for Chinese characters: [\U4E00-\U9FA5]
Double-byte characters: [^\x00-\xff] (including Chinese characters, which can be used to compute the length of a string (a double-byte character length meter 2,ascii character 1))
Regular expression for blank rows: \n\s*\r (can be used to delete blank lines)
Regular expression:< for HTML tags (\s*?) [^>]*>.*?</\1>|<.*? /> (online version is too bad, this can only be part of the complex nested tags still powerless)
Regular expressions for the end-end whitespace characters: ^\s*|\s*$ or (^\s*) | (\s*$) (can be used to delete whitespace characters (including spaces, tabs, page breaks, and so on) at the end of the line, very useful expressions)
Tencent QQ Number: [1-9][0-9]{4,} (Tencent QQ number starts from 10000)
China postal Code: [1-9]\d{5} (?! \d) (China postal Code is 6 digits)
IP address: \d+\.\d+\.\d+\.\d+ (useful when extracting IP addresses)
IP address: (?:(? : 25[0-5]|2[0-4]\\d| [01]?\\d?\\d) \.) {3} (?: 25[0-5]|2[0-4]\\d| [01]?\\d?\\d)] (provided by the @ Dragon three less, thanks for sharing)
The above is the real, the most comprehensive regular expression, I hope to help you learn some, quickly collection.