# The most practical regular expression collation

Time：2020-10-13

Author: moocnet
Source: Zhihu

## 1、 Check the expression of the number

``````1 digit: ^ [0-9]\$

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

3 at least n digits: ^ D {n,}\$

4 M-N digits: ^ D {m, n}\$

5 zero and nonzero start numbers: ^ (0| [1-9] [0-9] *)\$

6 digits with two decimal places starting from non-zero: ^ ([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 is a positive real number with two decimal places: ^ [0-9] + (. [0-9] {2})\$

10 a positive real number with 1-3 decimal places: ^ [0-9] + (. [0-9] {1,3})\$

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

12 nonzero negative integer: ^ - [1-9] [] 0-9 "* \$or ^ - [1-9] d*\$

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

14 non positive integer: ^ - [1-9] d * | 0 \$or ^ ((- D +)| (0 +))\$

15 nonnegative 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] * [1-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] * [1-9] [0-9] *))\$

19 floating point number: ^ (-? D +) (. D +)? \$or ^ -? ([1-9] d *. D * | 0. D * [1-9] d * | 0?. 0 + | 0)\$``````

## 2、 Expression of check character

``````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 string composed of 26 English letters: ^ [a-za-z]+\$

5 string consisting of 26 uppercase letters: ^ [A-Z]+\$

6 string consisting of 26 lowercase letters: ^ [A-Z]+\$

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

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

9 Chinese, English, numbers including underline: ^ [u4e00-u9fa5a-za-z0-9_ + \$

Chinese, English, numerals but excluding underscores: ^ [u4e00-u9fa5a-za-z0-9] + \$or ^ [u4e00-u9fa5a-za-z0-9] {2,20}\$

11 can input characters such as ^% & '; =? \$": [^% &'; =? \$X22]+

12 characters containing ~ are prohibited: [^ ~ X22]+``````

## 3、 Special requirement 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 numbers ("xxx-xxxxxx", "xxx-xxxxxxxx", "xxx-xxxxxxxx", "xxx-xxxxxxxx", "XXXXXX" and "XXXXXXXX): ^ ((D {3,4} -) | D {3.4} -)? D {7,8}\$

6 domestic telephone number (0511-4405222, 021-87888822): D {3} - D {8} | D {4} - D {7}

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

8 short ID card number (at the end of number and letter X): ^ ([0-9]) {7,18} (x | x)? \$or ^ D {8,18}| [0-9x] {8,18}| [0-9x] {8,18}\$

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

Password (start with a letter, length between 6 ~ 18, only contain letters, numbers and underscores): ^ [a-za-z] W {5,17}\$

Strong password (must contain a combination of upper and lower case letters and numbers, cannot 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}

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

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

There are four forms of money that we can accept: "10000.00" and "10000.00", and "10000" and "10000" without "points": ^ [1-9] [0-9]*\$

17 2. 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] *)\$

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

Let the user start with 0. Remove the negative sign, because money can't be negative. Next, we'll add the 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 passed, but "10" and "10.2" are passed: ^ [0-9] + (. [0-9] {2})\$

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})\$

This allows the user to write only one decimal place. Next, we should consider the comma in the number, we can: ^ [0-9] {1,3} (, [0-9] {3}) * (. [0-9] {1,2})\$

23, 8.1 to 3 numbers, followed by any comma + 3 numbers, the comma becomes optional and not required: ^ ([0-9] + | [0-9] {1,3} (, [0-9] {3}) *) (. [0-9] {1,2})\$

Note: This is the final result, don't forget "+" can be replaced by "*", if you think empty string is acceptable (strange, why?) finally, don't forget to remove the backslash when using the function, the general error is here

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

26 regular expression of Chinese characters: [u4e00-u9fa5]

Can be used to calculate the length of a double line character string (including the length of a double line of characters: [^ 27-2 bytes, including the length of a double line character string: [^ 2-2 bytes, including the length of a double line of characters: [^ 2-2 bytes)

29 regular expressions for HTML Tags: < (s *?) [^ >] * >. *? < / 1 > |. *? / > (the version circulated on the Internet is too bad, the above one is only partial, and it still can't do anything for complex nested tags) 30 regular expressions with white space characters: ^ s * | s * \$or (^ s *) (s * \$) (can be used to remove white space characters at the beginning and end of a line (including spaces, tabs, page breaks, etc.), very useful expressions)

QQ number of Tencent: [1-9] [0-9] {4,} (QQ number of Tencent starts from 10000)

32 China postcode: [1-9] d {5} (?! d) (China postcode is 6 digits)

33 IP address: D +. D +. D +. D + (useful for 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))

"^ D + \$" // non negative integer (positive integer + 0)

"^ [0-9] * [1-9] [0-9] * \$" // positive integer

"^ ((- D +)| (0 +)) \$" // non positive integer (negative integer + 0)

"^ - [0-9] * [1-9] [0-9] * \$" // negative integer

"^ -? D + \$" // integer

"^ D + (. D +)? \$" // non negative floating point number (positive float + 0)

"^ (([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] +) | ([0-9] * [1-9] [0-9] *) \$" // positive floating point number

"^ ((- D + (. D +)?)| (0 + (. 0 +)?)) \$" // non positive floating point number (negative floating point number + 0)

"^ (- ((([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] +) | ([0-9] * [1-9] [0-9] *)) \$" // negative floating point number

"^ (-? D +) (. D +)? \$" // floating point number

"^ [a-za-z] + \$" // a 26 letter string

"^ [A-Z] + \$" // a string consisting of 26 uppercase letters

"^ [A-Z] + \$" // a string of 26 lowercase letters

"^ [a-za-z0-9] + \$" // a string of numbers and 26 letters

"^ W + \$" // a string of numbers, 26 letters, or underscores

"^ [w -] + (. [w -] +) * @ [w -] + (. [w -] +) + \$" // email address

"^[a-zA-z]+://(w+(-w+)*)(.(w+(-w+)*))*(?S*)?\$" //url

Integer or decimal: ^ [0-9] +. {0,1} [0-9] {0,2}\$

You can only enter numbers: ^ [0-9] * \$.

You can only enter n-digit numbers: '^ D {n} \$".

You can only enter at least n digits: '^ D {n,} \$".

Only m ~ n digits can be input. "^d{m,n}\$"

You can only enter numbers beginning with zero and non-zero: '^ (0| [1-9] [0-9] *) \$".

Only positive real numbers with two decimal places can be entered: ^ [0-9] + (. [0-9] {2})? \$".

Only positive real numbers with 1-3 decimal places can be entered: ^ [0-9] + (. [0-9] {1,3})? \$".

You can only enter non-zero positive integers: ^ +? [1-9] [0-9] * \$".

You can only enter non-zero negative integers: ^ - [1-9] [] 0-9 "* \$.

Only characters of length 3 can be entered: ^. {3} \$".

You can only enter a string of 26 English letters: ^ [a-za-z] + \$.

You can only enter a string of 26 uppercase letters: ^ [A-Z] + \$.

You can only enter a string of 26 lowercase letters: ^ [A-Z] + \$.

You can only enter a string consisting of numbers and 26 English letters: ^ [a-za-z0-9] + \$.

You can only enter a string consisting of numbers, 26 English letters or underscores: ^ W + \$.

Verify the user password: ^ [a-za-z] W {5,17} \$"the correct format is: start with a letter, length is between 6 ~ 18, and can only contain characters, numbers and underscores.

Verify that it contains characters such as ^% & '',; =? \$": [^% & '',; =? \$X22] +".

You can only input Chinese characters: ^ [u4e00-u9fa5] {0,} \$"

Verify email address: '^ W + ([- +.] W +) * @ w + ([-.] W +) *. W + ([-.] W +) * \$".

Verify interneturl: "^ http: // ([w -] +.) + [w -] + (/ [w -. /?%, = *)? \$".

Verify that the phone number: ^ ((D {3,4} -) | D {3.4} -)? D {7,8} \$"is in the correct format:" xxx-xxxxxx "," xxx-xxxxxxxx "," xxx-xxxxxx "," xxx-xxxxxxxx ", and" XXXXXXXX ".

Verification ID number (15 or 18 digits): "^ D {15}| D {18} \$".

Verify the 12 months of a year: "^ (0? [1-9] | 1 [0-2]) \$" in the correct format: "01" ～ "09" and "1" ～ "12".

Verify the correct format of ^ ((0? [1-9]) | ((1| 2) [0-9]) | 30 | 31) \$"on the 31 days of a month

"01" ～ "09" and "1" ～ "31".

Integer or decimal: ^ [0-9] +. {0,1} [0-9] {0,2}\$

"^ W + \$" // a string of numbers, 26 letters, or underscores

"^ [w -] + (. [w -] +) * @ [w -] + (. [w -] +) + \$" // email address

"^[a-zA-z]+://(w+(-w+)*)(.(w+(-w+)*))*(?S*)?\$" //url

The input form is as follows: 2008, 2008-9, 2008-09, 2008-9-9, 2008-09-09. ^ (D {4}| (D {4} - D {1,2}) | (D {4} - D {1,2}) | (D {4} - D {1,2})\$

Mailbox validation regular expression W + ([- +.] W +) * @ w + ([-.] W +) *. W + ([-.] W +))*``````

## 4、 Application skills of network verification

• Verify e-mail format

`public bool IsEmail(string str_Email)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_Email,@"^([w-.]+)@(([−9]1,3.[−9]1,3.[−9]1,3.)|(([w−]+.)+))([a−zA−Z]2,4|[−9]1,3)(?)\$");` `}`

`public bool IPCheck(string IP)` `{
string num =` `“(25[0-5]|2[0-4]d|[0-1]d{2}|[1-9]?d)”;` `return` `Regex.IsMatch(IP,(“^”` `+ num +` `“.”` `+ num +` `“.”` `+ num +` `“.”` `+ num +` `“\$”));` `}`

• Verify URL

`public bool IsUrl(string str_url)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_url,` `@"http(s)?://([w-]+.)+[w-]+(/[w- ./?%&=]*)?");` `}`

``````5、 Common digital verification techniques
· verify phone number``````

`public bool IsTelephone(string str_telephone)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_telephone,` `@"^(d{3,4}-)?d{6,8}\$");` `}`

• Input password condition (characters and data appear at the same time)

`public bool IsPassword(string str_password)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_password,` `@"[A-Za-z]+[0-9]");` `}`

• Postal number

`public bool IsPostalcode(string str_postalcode)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_postalcode,` `@"^d{6}\$");` `}`

• phone number

`public bool IsHandset(string str_handset)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_handset,` `@"^[1]+[3,5]+d{9}\$");` `}`

• ID number

`public bool IsIDcard(string str_idcard)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_idcard,` `@"(^d{18}\$)|(^d{15}\$)");` `}`

• Two decimal places

`public bool IsDecimal(string str_decimal)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_decimal,` `@"^[0-9]+(.[0-9]{2})?\$");` `}`

• 12 months of the year

`public bool IsMonth(string str_Month)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_Month,` `@"^(0?[[1-9]|1[0-2])\$");` `}`

• 31 days of a month

`public bool IsDay(string str_day)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_day,` `@"^((0?[1-9])|((1|2)[0-9])|30|31)\$");` `}`

• Digital input

`public bool IsNumber(string str_number)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_number,` `@"^[0-9]*\$");` `}`

`public bool IsPasswLength(string str_Length)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_Length,` `@"^d{6,18}\$");` `}`

• Nonzero positive integer

`public bool IsIntNumber(string str_intNumber)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_intNumber,` `@"^+?[1-9][0-9]*\$");` `}`

``6、 Common character verification techniques``

## 1. Capital letters

`public bool IsUpChar(string str_UpChar)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_UpChar,` `@"^[A-Z]+\$");` `}`

``2. Lower case letters``

`public bool IsLowChar(string str_UpChar)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_UpChar,` `@"^[a-z]+\$");` `}`

``3. Check the repeated words in the string``

`private` `void btnWord_Click(object sender,` `EventArgs e)` `{` `System.Text.RegularExpressions.MatchCollection matches =` `System.Text.RegularExpressions.Regex.Matches(label1.Text,` `@"b(?<word>w+)s+(k<word>)b",` `System.Text.RegularExpressions.RegexOptions.Compiled` `|` `System.Text.RegularExpressions.RegexOptions.IgnoreCase);` `if` `(matches.Count` `!=` `0)` `{
foreach (System.Text.RegularExpressions.Match match in matches)` `{
string word = match.Groups[“word”].Value;` `MessageBox.Show ( word.ToString (), “English words”);` `}` `}` `else` `{` `MessageBox.Show (“no repeated words”);` `}` `}`

``4. Replace string``

`private` `void button1_Click(object sender,` `EventArgs e)` `{
string strResult =` `System.Text.RegularExpressions.Regex.Replace(textBox1.Text,` `@”[A-Za-z]*?”, textBox2.Text);` `MessageBox.Show (character before replacement:` `+` `“n”` `+ textBox1.Text` `+` `“n”` `+` `Replaced characters:` `+` `“n”` `+ textBox2.Text` `+` `“n”` `+` `Replaced characters:` `+` `“n”` `+Strresult, “replace”);` `}`

``5. Split string``

`private` `void button1_Click(object sender,` `EventArgs e)` `{``//Example: a 025-8343243, B 0755-2228382, C 029-32983298389289328932893289ding
foreach (string s in System.Text.RegularExpressions.Regex.Split(textBox1.Text,@”d{3,4}-d*”))` `{
textBox2.Text+=s;` `//Output “a, B, C, D” in turn` `}` `}`

``6. Verify the input letters``

`public bool IsLetter(string str_Letter)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_Letter,` `@"^[A-Za-z]+\$");` `}`

``7. Verify the input Chinese characters``

`public bool IsChinese(string str_chinese)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_chinese,` `@"^[u4e00-u9fa5],{0,}\$");` `}`

``8. Verify input string (at least 8 characters)``

`public bool IsLength(string str_Length)` `{` `return` `System.Text.RegularExpressions.Regex.IsMatch(str_Length,` `@"^.{8,}\$");` `}`

##### ES6 Unicode regular expression validation special characters (more Emoji)
``````The encoding of special characters will be greater than 65535 and will be between ffff and fffff
If (v.match (/ \ u {ffff} - \ u {fffff} / U)) {// indicates the compilation method of Unicode with ES6
return {
type:'ltFFF',
Message: "you have entered an illegal character"
}
}``````
###### ES6 matches characters that are not in Unicode characters
``````If (v.match (/ [[P {C}] / U)) {// P means match, C means other
return{
type:"noOther",
Message: "you have entered an illegal character"
}
}``````

## GMP principle and scheduling analysis of golang scheduler

Series of articles： – golang deeply understands GMP GMP principle and scheduling analysis of golang scheduler This paper mainly introduces the process and principle of goroutine scheduler in detail. It can have a clear understanding of the detailed scheduling process of go scheduler. It takes 4 days to make 30 + graphs (recommended Collection), including […]