Casually write regular expressions

First, the check digit of expression

    1, numeral: ^ [0-9] * $

    2, n bit digital: ^ d {n} $

    3, at least a number n of bits: ^ d {n,} $

    4, the digital mn bits: ^ d {m, n} $

    5, beginning with zero and non-zero numbers: ^ (0 | [1-9] [0-9] *) $

    6, beginning with zero up to two decimal places: ^ ([1-9] [0-9] *) $ + ([0-9] {1,2})?

    7 with 1-2 decimals positive or negative: ^ (-) d + (d {1,2}.) $??

    8, positive, negative, and decimals:?? ^ (- | +) d + (. D +) $

    9, there is a positive real number two decimals: (. [0-9] {2})? ^ [0-9] + $

    10, 1 to 3 decimal positive real number: (. [0-9] {1,3})? ^ [0-9] + $

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

    12, a non-zero negative integers: ^ - [1-9] [] 0-9 "or * ^ $ - [1-9] * $ D

    13, non-negative integers: ^ d + $ or ^ [1-9] d * | 0 $

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

    15, non-negative floating-point numbers: ^ d + (d +.) $ Or ^ [1-9] d * .d * | 0.d * [1-9] d * | 0 .0+ | 0 $??

    16, non-positive float: (.?.? (- d + (d +)) | (0 + (0 +))) ^ $ or ^ (- ([1-9] d * .d * | 0.d * [1-9] d *)) | 0 .0+ |? 0 $

    17, positive float: ^ [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 float: ^ - ([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 numbers: ^ (- d +?) $ Or ^ - ([1-9] d * .d * | 0.d * [1-9] d * | 0 .0+ (d +.)??? | 0) $

Second, check character expression

    1, Character: ^ [a - 龥] {0,} $

    2, alphanumeric: ^ [A-Za-z0-9] + $ or ^ [A-Za-z0-9] {4,40} $

    3, the length of all characters 3-20: {^} $ 3,20.

    4, a string consisting of letters 26: ^ [A-Za-z] + $

    5, a string of 26 English capital letters: ^ [AZ] + $

    6, letters written by a small string consisting of 26: ^ [az] + $

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

    8, string of digits, letters or underscore 26 consisting of: ^ w + $ or ^ w {3,20} $

    9, Chinese, English, numbers underlined comprising: ^ [a - 龥 A-Za-z0-9 _] + $

    10, Chinese, English, numbers, symbols such as but not underlined: ^ [a - 龥 A-Za-z0-9] + $ ^ or [a - 龥 A-Za-z0-9] {2,20} $

    11, may contain enter% ^ & '?,; = $ "Characters such as: [% ^ &'; = $?"] + ~ 12 inhibit input characters comprising: [~ ^ "] +

Third, the special needs of expression

    1、Email地址:^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$

    2, 域名: [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]* 或 ^http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$

    4, mobile 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, the phone number ( "XXX-XXXXXXX", "XXXX-XXXXXXXX", "XXX-XXXXXXX", "XXX-XXXXXXXX", "XXXXXXX" and "XXXXXXXX): ^ ((d {3,4} -) | d {3.4} -?) d {7,8} $

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

    7, the ID number (15, 18 digit): ^ d {15} | d {18} $

    8, the 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, account is valid (beginning with the letter, allowing 5-16 bytes to allow alphanumeric underlined): ^ [a-zA-Z] [a-zA-Z0-9 _] {4,15} $

    10, the password (start with a letter, a length between 6 and 18, can contain letters, numbers and underscores): ^ [a-zA-Z] w {5,17} $

    11, strong passwords (which must contain a combination of uppercase and lowercase letters and numbers, special characters can not be used, a length of between 8-10): ^ (?. = * D) (= (= * [az]?.)?. * [AZ]). {8,10} $

    12, Date format: ^ d {4} -d {1,2} -d {1,2}

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

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

Money input formats:

  1, representing four forms of money we can accept: "10000.00" and "10,000.00" and not "divide" and "10000" and "10,000": ^ [1-9] [0-9] * $    

  2, which means that an arbitrary number from 0 to begin with, but it also means that a character "0" is not passed, so we take the following form: ^ (0 | [1-9] [0-9] *) $

  3, not a 0 or a number from 0 to begin with we can also allow the beginning there is a minus: ^ (0 | - [1-9] [0-9] *?) $

  4, which represents a 0 or a possible beginning with a negative not a digital 0. allows users to start with 0 as well. The minus sign also removed, because the money can not be negative right. Now we want to add a description possible fractional part: ^ [0-9] + (. [0-9] +) $?

  5, it must be noted that, after the decimal point should be at least 1 digit, so "10" is not adopted, but "10" and "10.2" are adopted: ^ [0-9] + ([0-. 9] {2})? $

  6, so we must have regulations after the decimal point two, if you think too hard, it can be: (. [0-9] {1,2})? ^ [0-9] + $

  7, thus allowing the user to write only a decimal number following the comma we considered, we can:. ^ [0-9] {1,3} (, [0-9] {3}) * ( . [0-9] {1,2})? $

  8,1 to 3 digits, followed by comma +3 arbitrary numbers, commas become optional, rather than necessary: ​​^ ([0-9] + | [0-9] {1,3} (, [0 -9] {3}) *) (. [0-9] {1,2})? $

Note: This is the final result, do not forget the "+" can use the "*" alternative if you feel empty string can accept it (wonder why?) Finally, do not forget to remove the remove function when using the backslash bar, general errors are here

 

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

Chinese characters in regular expressions: [a - 龥]

Double Byte Character: [^ - ÿ] (including characters included, can be used to calculate the length of the string (a double-byte character length meter 2, ASCII characters are counted 1))

Blank line regular expression: s * (can be used to delete blank lines)

HTML markup regular expression:.?.? <(? S *) [^>] *> * </> | <* /> (circulated on the Internet version too bad, above this was only partially, for complex nested tags still powerless)

And last blank character of the regular expression: ^ s * | s * $ or (^ s *) | (s * $) (can be used to delete the row end of the first line blank characters (including spaces, tabs, page breaks etc.), very useful expression)

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

China Postal Code: [1-9] d {5} (?! d) (6-digit ZIP code for China)

IP address: d + .d + .d + .d + (useful for the extraction of the IP address)

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

Guess you like

Origin www.cnblogs.com/buwngchuxin/p/10978047.html