Home >headlines >99+ commonly used regular expressions, enough for free!

99+ commonly used regular expressions, enough for free!

青灯夜游
青灯夜游forward
2022-07-19 11:10:422747browse

Regular describes a string matching pattern, but for many people writing regular is "Landry's torture". If you don't need to use it frequently, you won't even think about learning it. This article will sort out and share with you some commonly used regular expressions. Do you want to use regular expressions for free? Come and collect it!

You want to have sex for free, right? This article will give you enough at one time! Rush 99 first! (If you still think it’s not enough, just leave a comment and add more. This article will continue to be updated and added!!)

Another little tip, students who have difficulty reading can use this websitejex.im/ regulex/, visualize regular rules and fall in love with reading regular rules!

For example: regular rules for verifying email addresses: ^\w ([- .]\w )*@\w ([-.]\w )*\.\w ([-.] \w )*$

99+ commonly used regular expressions, enough for free!

After visualization, just look at the pictures and talk, novices can do it, it’s really tql! !

Okay, let’s rush!

Number related

  • Mobile phone number (starting with 1): /^(?:(?:\ |00)86 )?1\d{10}$/

  • Mobile phone number (starting with 13 to 19): /^(?:(?:\ |00) 86)?1[3-9]\d{9}$/

  • Mobile phone number (starting with the mobile phone number range announced by the Ministry of Industry and Information Technology): /^( ?:(?:\ |00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])| (?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/

  • Domestic landline number: /\d{3}-\d{8}|\d{4}-\d{7}/

  • Email number: /^\w ([- .]\w )*@\w ([-.]\w )*\.\w ([-. ]\w )*$/

  • Postal code: /[1-9]\d{5}(?!\d)/

  • ID number: /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1 -9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/

  • Bank card number (public and private account): /^[1-9]\d{9,29}$/

  • License number: /^[Beijing, Tianjin, Shanghai, Chongqing, Hebei, Yunnan, Liaoning, Hunan, Anhui, Shandong, New Jiangsu, Zhejiang, Jiangxi, Hubei, Guangxi, Gansu, Shanxi, Mongolia, Shaanxi, Jilin, Fujian, Guangdong, Qinghai, Tibet, Sichuan, Ningqiong, Ambassador][A-HJ-NP-Z][A -HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9 HONG KONG and MACAO]$/

  • ##QQ number:

    /^[1-9][0-9]{4,10}$/

  • WeChat ID:

    /^[a-zA-Z ][-_a-zA-Z0-9]{5,19}$/

  • Version number (x.y.z):

    /^\d (?:\ .\d ){2}$/

  • ##Legal account number 1 (starting with a letter, 5-16 digits, alphanumeric and underscore allowed):
  • /^[a- zA-Z][a-zA-Z0-9_]{4,15}$/

  • Legal account number 2 (4-16 digits, letters, numbers, and underscores allowed) , minus sign):
  • /^[a-zA-Z0-9_-]{4,16}$/

  • Strong password 1 (must contain uppercase and lowercase letters A combination of letters and numbers, special characters cannot be used, the length is between 8-10):
  • /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z] ).{8,10}$/

  • Strong password 2 (must contain letters, numbers, special characters: **@#$%^&
  • `~ ()- =

    * ):

  • ##/^(?![a-zA-Z] $)(?![A-Z0-9] $ )(?![A-Z\W_!@#$%^&* ~()- =] $)(?![a-z0-9] $)(?![a-z\\W_!@#$%^ & *~()- =] $)(?![0-9\W_!@#$%^&* ~()- =] $)[a-zA-Z0-9\\W_!@#$ %^&*~()- =]/

URL:
    /^(((ht|f)tps?):\/\/)? (^!@#$%^&*?.\s-?\.) [a-z]{2,6}\/?/
  • URL with port number :
  • /^((ht|f)tps?:\/\/)?[\w-] (\.[\w-] ) :\d{1,5}\/?$/
  • ip-v4:
  • /\\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/
  • ip-v6:
  • /(([0-9a-fA-F]{1 ,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|( [0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1, 4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){ 1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}( :[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a -fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}) {1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0 ,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}( (25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25 [0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1 ,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9] )\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])) /
  • Number related

Only numbers:
    /^[0-9]* $/
  • or

    /^\d{1,}$/

    Integer:
  • /^-?[0-9]\d *$/
  • Positive integer:
  • /^\ ?[1-9]\d*$/
  • ##Non-positive integers:

    /^-[1-9]\d*|0$/
  • Negative integers:

    /^-[ 1-9]\d*$/
  • ##Non-negative integer: /^\d $/

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

  • ##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*)/

  • Decimal: /^-?\d \.\d $/

  • Positive/Negative/Decimal: /^(\-|\ ) ?\d (\.\d )?$/

  • Positive real numbers retain 2 decimal places: /^[0-9] (.[0- 9]{2})?$/

  • Positive real numbers retain 1 to 3 digits after the decimal point: /^[0-9] (.[0-9]{1,3})?$/

  • n digits: /^\d{n}$/

  • At least n digits: / ^\d{n,}$/

  • Numbers from m to n digits: /^\d{m,n}$/

  • Contains at least one of the numbers and letters: /^[A-Za-z0-9] $/

  • Must Contains numbers and letters: /^(?=.*[a-zA-Z])(?=.*\d). $/

  • ##md5 Value:

    /^([a-f\d]{32}|[A-F\d]{32})$/

  • ##base64 Value:
  • / ^\s*data:(?:[a-z] \/[a-z0-9- .] (?:;[a-z-] =[a-z0-9-] )?)?(?:;base64) ?,([a-z0-9!$&',()* ;=\-._~:@/?%\s]*?)\s*$/i

Character related

    Characters from m to n digits:
  • /^.{3,20}$/

  • English alphabetic characters:
  • /^[A-Za-z] $/

  • Uppercase English alphabetic characters:
  • /^[A-Z] $/

    ##Lowercase English alphabetic characters:
  • /^[a-z] $/
  • Chinese characters:
  • /^[\u4e00-\u9fa5]{0,}$/
  • Full-width symbols:
  • /[\uFF00- \uFFFF]/
  • ##Half-width symbols:

    /[\u0000-\u00FF]/
  • Chinese characters , at least one of English, numbers, and underscores:

    /^[\u4E00-\u9FA5A-Za-z0-9_] $/
  • does not contain the character "~ ”:

    /[^~\x22] /
  • Characters are repeated continuously:

    /(.)\1 /
  • ⌚Time related

24-hour time (HH:mm:ss):

/^(?:[01 ]\d|2[0-3]):[0-5]\d:[0-5]\d$/
  • 12 hour time (hh :mm:ss):

    /^(?:1[0-2]|0?[1-9]):[0-5]\d:[0-5]\d$/
  • 24-hour time (HHmmss):

    /([0-1]?[0-9]|2[0-3])([0-5] [0-9])([0-5][0-9])$/
  • Date 1 (yyyy-MM-dd, such as 2222-01-01 , the year must be 4 digits):

    /^\d{4}-\d{1,2}-\d{1,2}/
  • Date 2 (such as 333-01-01, the year can be less than 4 digits):

    /^\d{1,4}(-)(1[0-2]|0?[1-9])\1 (0?[1-9]|[1-2]\d|30|31)$/
  • Date 3 (yyyyMMdd, such as 20220202):

    /^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[ 1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12 ][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1 -9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[ 13579][26])|((0[48]|[2468][048]|[3579][26])00))0229))$/
  • Date time 1 (YYYYMMDD HH:mm:ss):

    /^\d{4}([/:-\S])(1[0-2]|0?[1-9])\1 (0?[1-9]|[1-2]\d|30|31) (?:[01]\d|2[0-3]):[0-5]\d:[0-5 ]\d$/
  • Date Time 2:

    /^[1-9]\d{3}-(0[1-9]|1[ 0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s (20|21|22|23|[0-1]\d ):[0-5]\d:[0-5]\d$/
  • 12 months a year ((01~09 or 1~12)) :

    /^(0?[1-9]|1[0-2])$/
  • 31 days in a month (01~09 or 1~ 31):

    /^((0?[1-9])|((1|2)[0-9])|30|31)$/
  • A month with 31 days:

    /^(0?[13578]|1[02])$/
  • ##A month with 30 days: /(0[469]|11)-(0[1-9]|[12][0-9]|30)/

  • 2 months 28 days ("02-28"): /^02-(0[1-9]|[1][0-9]|2[0-8])$/

  • Leap year:/^(((19|20)([13579][26]|[2468][048]|0[48]))|(2000))$/

  • February in a leap year (such as 2008-02-01): /^(((19|20)([13579][26]|[2468] [048]|0[48]))|(2000))-0?2-(0?[1-9]|[12]\d)$/

  • Date (including leap year, big and small month judgment): /((((19|20)\d{2})-(0?(1|[3-9])|1[012]) -(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)| (((19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([ 13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$/

  • Year Interval-year (such as 19 to 20 years): /^((19|20)\d{2})$/

  • ##Year interval-year Month (such as 1999-01): /^((((19|20)\d{2})-(0?[13-9]|1[012]))|(((19|20 )\d{2})-(0?[13578]|1[02]))|(((19|20)\d{2})-0?2)|((((19|20)( [13579][26]|[2468][048]|0[48]))|(2000))-0?2))$/

  • Year range -Year, month and day (such as 1999-01-01): /^((((19|20)\d{2})-(0?[13-9]|1[012])-(0 ?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|((( 19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((19|20)([13579][ 26]|[2468][048]|0[48]))|(2000))-0?2-29))$/.test('2021-02-21')$/

  • Year interval - year, month and day (the interval symbol can be - / or empty): /^(?:(?:1[6-9]|[2-9][ 0-9])[0-9]{2}([-/.]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[ 1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(? :0?[13578]|1[02])\1(?:31))|(?:(?:1[6-9]|[2-9][0-9])(?:0[ 48]|[2468][048]|[13579][26])|(?:16|[2468][048]|[3579][26])00)([-/.]?)0?2 \2(?:29))$/

  • Programming related

Hexadecimal color: /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/

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

  • Video link address: /^https?:\/\/(. \/) . (\.(swf|avi|flv|mpg|rm|mov|wav|asf |3gp|mkv|rmvb|mp4))$/i

  • Picture link address:/^https?:\/\/(. \/) . (\.(gif|png|jpg|jpeg|webp|svg|psd|bmp|tif))$/i

  • ##mac Address:

    /^( (([a-f0-9]{2}:){5})|(([a-f0-9]{2}-){5}))[a-f0-9]{2}$/ i

  • Subnet mask:

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

  • File extension validation:

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

  • java package name (x.x.x):

    /^([a-zA-Z_]\w*) ([.][a-zA-Z_]\w*) $/

  • xml file:

    /^([a-zA-Z] -?) [a-zA-Z0-9] \\.[x|X][m|M ][l|L]$/

  • html Comment:

    //g

  • html Tag 1:

    /]*>(.*?)?/

  • html Tag 2:

    /]*>.*?\1>|/

  • ## Leading and trailing blank characters:
  • /^\s*|\s*$/

  • Find CSS properties:
  • /^\\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:
  • /\\]*[ src] *= *[\\"\\']{0,1}([^\\"\\'\\ >]*)/

  • Thunder link:
  • /^thunder:\/\/[a-zA-Z0-9] =$/

  • ed2k link:
  • /^ed2k :\/\/|file|. |\/$/

  • linux "file" path:
  • /^\/(\w \/) \w \.\w $/

  • "File" path under window:
  • /^[a-zA-Z]:\\(?:\w \\ )*\w \.\w $/

life-related

    amount (loose, optional is negative, the first bit can be 0, supports thousandth separation):
  • /^-?\d (,\d{3})*(\.\d{1,2})?$/

  • Amount (greater than 0, two decimal places):
  • /(^[1-9]{1}[0-9]*$)|(^[0- 9]*\.[0-9]{2}$)/

  • Amount (strict, not negative, up to two decimal places, the first digit is not 0) :
  • /(^[1-9]([0-9] )?(\.[0-9]{1,2})?$)|(^(0){1}$)|( ^[0-9]\.[0-9]([0-9])?$)/

  • Passport:
  • /(^[EeKkGgDdSsPpHh] \d{8}$)|(^(([Ee][a-fA-F])|([DdSsPp][Ee])|([Kk][Jj])|([Mm][Aa]) |(1[45]))\d{7}$)/

  • Hong Kong ID card:
  • /^[a-zA-Z]\d{ 6}\([\dA]\)$/

  • Macau ID card:
  • /^[1|5|7]\d{6}\( \d\)$/

  • Wanwan ID card:
  • /^[a-zA-Z][0-9]{9}$/

  • Stock code:
  • /^(s[hz]|S[HZ])(000[\d]{3}|002[\d]{3} |300[\d]{3}|600[\d]{3}|60[\d]{4})$/

    ##Words that do not contain abc :
  • /\b((?!abc)\w) \b/
  • 100. To be continued...
  • ##Summary:

This article briefly organizes a wave of regular rules, many of which are also learned while reading, with the help of jex .im/regulex/, awesome!

If you have anything to add, please leave a comment, or if you find any errors, please point them out and they will be corrected immediately. I hope it can be helpful to your regular learning and use~

Original address: https://juejin.cn/post/7119242343798013959

More programming related For knowledge, please visit:

programming teaching

! !
Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete