Common regular expressions are more practical

Time:2021-8-2

Regular expression matching Chinese characters: [\ u4e00 – \ u9fa5]
Comment: matching Chinese is really a headache. It’s easy to do with this expression

Matching double byte characters (including Chinese characters): [^ \ X00 – \ XFF]
Comment: can be used to calculate the length of a string (a double byte character length meter 2, ASCII character meter 1)

Regular expressions that match blank lines: \ n \ s * \ R
Comment: can be used to delete blank lines

Regular expressions matching HTML Tags: < (\ s *?) [^ >] * >. *</\ 1>|<.*? />
Comment: the version circulated on the Internet is too bad. The above one can only match the part. It is still powerless for complex nested tags

Regular expression matching leading and trailing whitespace characters: ^ \ s * | \ s*$
Comment: it can be used to delete white space characters at the beginning and end of a line (including spaces, tabs, page breaks, etc.), which is a very useful expression

Regular expression matching email address: \ W + ([- +.] \ W +) * @ \ W + ([-.] \ W +) * \. \ W + ([-.] \ W +)*
Comment: form validation is useful

Regular expression matching URL of web address: [a-za-z] +: / / [^ \ S]*
Commentary: the functions of the version circulated on the Internet are very limited, and the above can basically meet the needs

Whether the matching 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}$
Comment: form validation is useful

Match domestic phone number: \ D {3} – \ D {8}| \ D {4} – \ D {7}
Comment: the matching form is 0511-4405222 or 021-87888822

Match Tencent QQ number: [1-9] [0-9] {4,}
Comment: Tencent QQ starts from 10000

Match China Postal Code: [1-9] \ d{5} (?)\ d)
Commentary: the postal code of China is 6 digits

Matching ID card: \ D {15}| \ D {18}
Commentary: China’s ID card is 15 or 18

Matching IP addresses: \ D + \. \ D + \. \ D + \. \ D+
Comment: useful when extracting IP addresses

Match specific numbers:
^[1-9] \ d * $/ / match positive integer
^-[1-9] \ d * $/ / match negative integer
^-?[ 1-9] \ d * $/ / match integer
^[1-9] \ d * |0 $/ / match non negative integer (positive integer + 0)
^-[1-9] \ d * |0 $/ / match non positive integers (negative integers + 0)
^[1-9] \ d * \. \ d * |0 \. \ d * [1-9] \ d * $/ / match positive floating point numbers
^-([1-9] \ d * \. \ d * |0 \. \ d * [1-9] \ d *) $/ / match negative floating-point number
^-?([ 1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\. 0 + |0) $/ / match floating point number
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\. 0 + |0 $/ / match non negative floating point number (positive floating point number + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\. 0 + |0 $/ / match non positive floating point numbers (negative floating point numbers + 0)
Commentary: it is useful when dealing with a large amount of data. Pay attention to correction in specific application

Match a specific string:
^[a-za-z] + $/ / matches a string of 26 English letters
^[A-Z] + $/ / matches a string composed of 26 uppercase letters
^[A-Z] + $/ / matches a string consisting of 26 lowercase letters
^[a-za-z0-9] + $/ / matches a string consisting of numbers and 26 English letters
^\W + $/ / matches a string consisting of numbers, 26 English letters, or underscores
Commentary: some of the most basic and commonly used expressions