Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write regular expressions

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to write regular expressions, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

one。 Check number

Number: ^ [0-9] * $

N-digit number: ^\ d {n} $

At least n-digit number: ^\ d {n,} $

The number of mmurn digits: ^\ d {mrecom n} $

Zero and non-zero numbers: ^ (0 | [1-9] [0-9] *) $

A number with up to two decimal places at the beginning of a non-zero: ^ ([1-9] [0-9] *) + (. [0-9] {1J2})? $

Positive or negative numbers with 1-2 decimal places: ^ (\ -)?\ d + (\. D {1jin2})? $

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 to 3 decimal places: ^ [0-9] + (. [0-9] {1jue 3})? $

A positive integer that is not zero: ^ [1-9]\ dbath $

Non-zero negative integer: ^\-[1-9] [] 0-9 "* $

Non-negative integer: ^\ dBy $

Non-positive integer: ^-[1-9]\ d* | 0 $

Non-negative floating point number: ^\ d + (\.\ d +)? $

Non-positive floating point number: ^ ((-\ d + (\.\ d +)?) | (0 + (\ .0 +)?)) $

Positive floating point number: ^ [1-9] d.d | 0.d [1-9] d $

Negative floating point number: ^-([1-9]\ d *.\ d * | 0\.\ d * [1-9]\ d *) $

Floating point number: ^ (-?\ d +) (\.\ d +)? $

two。 Validated character

Chinese character: ^ [\ u4e00 -\ u9fa5] {0,} $

English and numbers: ^ [A-Za-z0-9] + $

All characters with a length of 3-20: ^. {3pm 20} $

A string of 26 letters: ^ [A-Za-z] + $

A string of 26 uppercase letters: ^ [Amurz] + $

A string of 26 lowercase letters: ^ [amurz] + $

A string of numbers and 26 letters: ^ [A-Za-z0-9] + $

A string consisting of numbers, 26 letters, or underscores: ^\ wicked $

Chinese, English, numbers including underscore: ^ [\ u4E00 -\ u9FA5A-Za-z0-9] + $

Chinese, English, numbers but excluding underscores and other symbols: ^ [\ u4E00 -\ u9FA5A-Za-z0-9] + $

You can enter characters such as ^% &',; =? $\ ": [^% &',; =? $\ x22] +

Prohibited from entering characters containing ~: [^ ~\ x22] +

three。 Special requirements expression

Email address: ^\ w + ([- +.]\ w +) * @\ w + ([-.]\ w +) *.\ w + ([-.]\ w +) * $

Mobile number: ^ (13 [0-9] | 14 [5 | 7] | 15 [0 | 1 | 2 | 3 | 6 | 7 | 8 | 9] | 18 [0 | 1 | 2 | 5 | 6 | 7 | 8 | 9])\\ d {8} $(regular expressions of domestic mobile numbers starting with 13, 15 and 18, the first two digits can be expanded according to the current domestic collection number)

Phone numbers ("XXX-XXXXXXX", "XXXX-XXXXXXXX", "XXX-XXXXXXX", "XXX-XXXXXXXX", "XXXXXXX" and "XXXXXXXX"): ^ (\ (\ d {3Magin4} -) |\ d {3.4} -)?\ d {7pc8} $

Domestic telephone number (0511-4405222, 021-87888822):\ d {3} -\ d {8} |\ d {4} -\ d {7}

ID card number (15 digits): ^ [1-9]\\ d {7} ((0\\ d) | (1 [0-2])) (([0 | 1 | 2]\\ d) | 3 [0-1])\\ d {3} $

ID card number (18 digits): ^ [1-9]\\ d {5} [1-9]\\ d {3} ((0\\ d) | (1 [0-2])) ([0 | 1 | 2]\\ d) | 3 [0-1])\\ d {3} ([0-9] | X) $

Whether the account is legal (starting with letters, allowing 5-16 bytes, allowing alphanumeric underscores): ^ [a-zA-Z] [a-zA-Z0-9 _] {4jue 15} $

Password (begins with a letter, is between 6 and 18 in length, and can only contain letters, numbers and underscores): ^ [a-zA-Z]\ w {5Magne17} $

Strong password (must contain a combination of uppercase and lowercase letters and numbers, cannot use special characters, length is between 8 and 16): ^ (? =. *\ d) (? =. * [Amurz]) (? =. * [Amurz]). {8jue 16} $

Date format: ^\ d {4} -\ d {1pc2} -\ d {1pc2}

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

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

Xml file: ^ ([a-zA-Z] + -?) + [a-zA-Z0-9] +\. [X | X] [m | M] [l | L] $

Regular expressions for Chinese characters: [\ u4e00 -\ u9fa5]

Double-byte characters: [^\ X00 -\ xff] (including Chinese characters, can be used to calculate the length of a string (a double-byte character length meter 2)

Regular expression for blank lines:\ n\ s*\ r (can be used to delete blank lines)

Regular expression of leading and trailing white space characters: ^\ swords $or (^\ s*) | (\ sblank $) (can be used to delete white space characters (including spaces, tabs, page feeds, etc.) at the beginning and end of a line, a very useful expression)

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

China Postal Code: [1-9]\ d {5} (?!\ d) (China Postal Code is 6 digits)

IP-v4 address:\\ b (?: 25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?)\\.) {3} (?: 25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?)\ b (useful when extracting IP addresses)

Verify IP-v6 address: ([0-9a-fA-F] {1 9a-fA-F 4}:) {7 9a-fA-F 7} [0-9a-fA-F] {1 4} | ([0-9a-fA-F] {1 9a-fA-F 4}:) {1 7}: ([0-9a-fA-F] {1 9a-fA-F 4}:) {1 6}: [0-9a-fA-F] {1 Piao 4} | ([0-9a-fA-F] {1 Person4}:) {1Pert 5} (: [0-9a-fA-F]) ] {1 9a-fA-F 4}) {1 9a-fA-F 2} | ([0-9a-fA-F] {1 9a-fA-F 4}:) {1 9a-fA-F 4} (: [0-9a-fA-F] {1 9a-fA-F 4}) {1 9a-fA-F 3} (: [0-9a-fA-F] {1 9a-fA-F 4}) {1 9a-fA-F 4} | ([0-9a-fA-F] {1 pm 4}:) {1 Mague 2} (: [0-9a-fA-F] {1 pm 4}) {1pm 4} | [0-9a-fA-F] {1 9a-fA-F 4}: (: [0-9a-fA-F] {1 9a-zA-Z 4}) {1 9a-zA-Z 6}) | (: [0-9a-zA-Z] {1 9a-zA-Z 4}) {1 9a-zA-Z 7} |:) | (: [0-9a-zA-Z] {0-9a-zA-Z] {1) | |:: (ffff (: 0 {0 9a-fA-F 4}) {0Power1}:) {0ffff 1} ((25 [0-5]) | (2 [0-4] | 1 {0Power1} [0-9]) {0Magne1} [0-9]) {0Magne1} (25 [0-5] | (2 [0-4] | 1 {0quotient 1} [0-9]) {0jin1} [0-9]) | ([0-0] 1}:) {1pm 4}: (25) [0-5] | (2 [0-4] | 1 {0rect 1} [0-9]) {0rect 1} [0-9])\\.) {3jue 3} (25 [0-5] | (2 [0-4] | 1 {0jue 1} [0-9]))

Subnet mask: (?: 25 [0-5] | 2 [0-4]\ d | [01]?\\ d?\\ d)\\.) {3} (?: 25 [0-5] | 2 [0-4]\ d | [01]?\\ d?\\ d)

Verification date: ^ (?: (! 0000) [0-9] {4}-(: 0 [1-9] | 1 [0-2])-(: 0 [1-9] | 1 [0-9] | 2 [0-8]) | (?: 0 [13-9] | 1 [0-2])-(?: 29 | 30) | (?: 0 [13578] | 1 [02])-31) | (?: [0-9] {2} (?) [48] | [2468] [048] | [13579] [26]) | (?: 0 [48] | [2468] [048] | [13579] [26]) 00)-02-29) $(date check in "yyyy-mm-dd" format A flat leap year has been considered.)

Extract comments:

Find the CSS attribute: ^\\ s * [a-zA-Z\\ -] +\\ s * [:] {1}\\ s [a-zA-Z0-9\\ s.#] + [;] {1}

Extract page hyperlink: (] *) (href= "https?:\\ /\ /) ((?! (: (?): (?: www\.)?'. Implode ('| (?: www\\.)', $follow_list).)) [^" rel= "external nofollow"] +) "((?!. *\\ brel=) [^ >] *) (?: [^ >] *) >

Extract web page images:\

< *[img][^\\\\>

] * [src] * = * [\ "\'] {0reco1} ([^\\"\\'\ >] *)

Extract web page color code: ^ # ([A-Fa-f0-9] {6} | [A-Fa-f0-9] {3}) $

File extension validity: ^ ([a-zA-Z]\\: |\)\ ([^\] +\) * [^\ /: *? "|] +\ .txt (l)? $

Judge the IE version: ^. * MSIE [5-8] (?:\. [0-9] +)? (?!. * Trident\ / [5-9]\ .0). * $

The above is all the content of the article "how to write regular expressions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report