Analysis of golang regular expression

Time:2021-2-26

Go (also known as golang) is a static, strongly typed and compiled language developed by Robert Griesemer, rob pike and Ken Thompson of Google. The syntax of go language is similar to that of C, but its functions include: memory security, GC (garbage collection), structure and CSP style concurrent computing.

Robert Griesemer, rob pike and Ken Thompson started to design go in September 2007. Later, Ian lance Taylor and Russ Cox joined the project. Go is based on Inferno operating system. Go was officially announced in November 2009 as an open source project, which was implemented on Linux and Mac OS X platforms, and then added the implementation under Windows system. In 2016, go was selected as “the best language of tiobe 2016” by software evaluation company tiobe. At present, go releases a secondary version every six months (that is, upgrading from a.x to a.y).

Let’s take a look at golang regular expressions.

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 numbers beginning with zero and non-zero: ^ (0 | [1-9] [0-9] *)$
6 non zero number with two decimal places at most: ^ ([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 numbers: ^ (\ – | \ +)? D + (\. [D +)$
9 positive real number with two decimal places: ^ [0-9] + (. [0-9] {2})$
Positive real numbers with 1-3 decimal places: ^ [0-9] + (. [0-9] {1,3})$
11 non zero positive integer: ^ [1-9] [D * $or ^ ([1-9] [0-9] *) {1,3} $or ^ \ +? [1-9] [0-9]*$
12 nonzero negative integers: ^ – [1-9] [] 0-9 “* $or ^ – [1-9] [D*$
13 non negative integer: ^ / D + $or ^ [1-9] [D * | 0$
14 non positive integer: ^ – [1-9] [D * | 0 $or ^ ((- [D +)| (0 +))$
15 non negative floating-point number: ^ ﹣ D + (\. [D +)? $or ^ [1-9] [D * \. [D * | 0 \. [D * [1-9] [D * | 0? \. 0 + | 0$
16 nonpositive 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 number: ^ (-? [D +) (\. [D +)? $or ^ -? ([1-9] [D * \. [D * | 0 \. [D * [1-9] [D * | 0? \. 0 + | 0)$

2、 Check the expression of the character

1 Chinese characters: ^ [[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 upper case English letters: ^ [A-Z]+$
6 a string of 26 lowercase English letters: ^ [A-Z]+$
7 a string of numbers and 26 letters: ^ [a-za-z0-9]+$
8 a string of numbers, 26 letters, or underscores: ^ / W + $or ^ / w {3,20}$
9 Chinese, English, numbers including underscores: ^ [[u4e00 – [u9fa5a-za-z0-9]_ ]+$
10 Chinese, English, numbers but excluding underscores and other symbols: ^ [[u4e00 – [u9fa5a-za-z0-9] + $or ^ [[u4e00 – [u9fa5a-za-z0-9] {2,20}$
11. You can input characters such as: ^% & ‘; =? $\ “: [^% &’; =? $[X22] + 12. You are not allowed to input characters containing: ^ ~ [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 Internet URL: [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 | 6 | 7 | 8 | 9]) d {8}$
5 telephone numbers (“xxx-xxxxxx”, “xxx-xxxxxxxx”, “xxx-xxxxxxxx”, “xxx-xxxxxxxx”, “XXXXXXXX” and “XXXXXXXX”): ^ (\ ([D {3,4} -) | [D {3.4} -)?)? [D {7,8}$
6 domestic telephone numbers (0511-4405222, 021-87888822): (D {3} – [D {8} | [D {4} – [D {7})
7 ID number (15 digit, 18 digit):^\d{15}|\d{18}$
8 short ID number (number, letter X End): ([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 (starting with a letter, 5-16 bytes are allowed, and alphanumeric underscores are allowed): ^ [a-za-z] [a-za-z0-9_ ]{4,15}$
10 password (starts with a letter, length between 6 and 18, can only contain letters, numbers and underscores): ^ [a-za-z] \ w {5,17}$
11 strong password (must contain the combination of uppercase and lowercase 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}
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 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 zero or a number that does not start with zero. We can also allow a negative sign at the beginning: ^ (0 | -? [1-9] [0-9] *)$
19 4. This means a zero or a number that may be negative but does not start with zero. Let the user start with 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 passed, but “10” and “10.2” are passed: ^ [0-9] + (. [0-9] {2})$
If you think it’s too harsh, you can do this: ^ [0-9] + (. [0-9] {1,2})$
22 7. This allows users to write only one decimal place. Next, we should consider the comma in the number. We can do this: ^ [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, not necessary: ^ ([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 the empty string is acceptable (strange, why?) finally, don’t forget to remove the backslash when using the function, the general errors are here
25 XML file: ^ ([a-za-z] + -?) + [a-za-z0-9] + \ \. [x | x] [M | M] [l | l]$
26 regular expressions of Chinese characters: [[u4e00 – [u9fa5]
27 double byte characters: [^ X00 – [XFF] (including Chinese characters, it can be used to calculate the length of string (a double byte character length is 2, ASCII character length is 1))
28 regular expression for blank line (can be used to delete blank line)
29 regular expressions of HTML Tags: < (< s *?) [^ >] * >. *? | <. *? / > (the version circulated on the Internet is too bad, and the above one can only be part of it, but it is still powerless for complex nested tags.)
Regular expression of beginning and end white space characters: ^ | [s * $or (^ | [s * $) (can be used to delete the white space characters at the beginning and end of a line (including spaces, tabs, page breaks, etc.), very useful expression)
31 Tencent QQ number: [1-9] [0-9] {4,} (Tencent QQ number starts from 10000)
32 postcode of China: [1-9] [D {5} (?)! (6-digit postcode of China)
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? \ \))

This article about the analysis of golang regular expression is introduced here. For more related content of golang regular expression, please search previous articles of developer or continue to browse the following related articles. I hope you can support developer more in the future!