Several commonly used regular expressions [go]

Source: Internet
Author: User

Only 1 digits can be entered

Expression: ^\d$
Description: Matches a number
Example of a match: 0,1,2,3
Examples of mismatches

Only n digits can be entered

Expression: ^\d{n}$ For example ^\d{8}$
Description: Matches 8 digits
Example of a match: 12345678,22223334,12344321
Examples of mismatches


You can only enter at least n digits

Expression: ^\d{n,}$ For example ^\d{8,}$
Description: Matches a minimum of n numbers
Example of a match: 12345678,123456789,12344321
Examples of mismatches


only m to n digits can be entered

Expression: ^\d{m,n}$ For example ^\d{7,8}$
Description: Matches m to n numbers
Example of a match: 12345678,1234567
Examples of mismatches: 123456,123456789


Only numbers can be entered

Expression: ^[0-9]*$
Description: Matches any number
Example of a match: 12345678,1234567
Examples of mismatches: E,


Only one interval number can be entered

Expression: ^[12-15]$
Description: A number that matches an interval
Example of a match: 12,13,14,15
Examples of mismatches:


Only 0 and non-0 digits can be entered.

Expression: ^ (0|[ 1-9][0-9]*) $
Description: Can be 0, the first number cannot be 0, the number can have 0
Example of a match: 12,10,101,100
Examples of mismatches: 01,


Only real numbers can be entered

Expression: ^[-+]?\d+ (\.\d+)? $
Description: Match real number
Example of a match: 18,+3.14,-9.90
Examples of mismatches:. 6,33s,67-99


Only positive real numbers with N decimal places can be entered

Expression: ^[0-9]+ (. [ 0-9]{n})? $ with ^[0-9]+ (. [ 0-9]{2})? $ for example
Description: Matches positive real numbers of n decimal places
Examples of matches: 2.22
Examples of mismatches: 2.222,-2.22,


Only positive real numbers with m-n decimal places can be entered

Expression: ^[0-9]+ (. [ 0-9]{m,n})? $ with ^[0-9]+ (. [ 0-9]{1,2})? $ for example
Description: A positive real number that matches m to n decimal places
Example of a match: 2.22,2.2
Examples of mismatches: 2.222,-2.2222,

You can only enter a positive integer that is not 0

Expression: ^\+? [1-9] [0-9]*$
Description: Matches a positive integer that is not 0
Example of a match: 2,23,234
Examples of mismatches: 0,-4,


You can only enter a negative integer that is not 0

Expression: ^\-[1-9][0-9]*$
Description: Matches a negative integer that is not 0
Example of a match: -2,-23,-234
Examples of mismatches: 0, 4,


Only n characters can be entered

Expression: ^. {n}$ with ^. {4}$ as an example
Description: Matches n characters, note Chinese characters are only 1 characters
Examples of matches: 1234,12we,123 Qing, qingqing Moon
Examples of mismatches: 0,123,123www,


Only English characters can be entered

Expression: ^. [A-za-z]+$ For example
Description: Match English characters, uppercase and lowercase
Examples of matches: Asp,www,
Examples of mismatches: 0,123,123www,


Only uppercase English characters can be entered

Expression: ^. [A-z]+$ For example
Description: Match English uppercase characters
Examples of matches: Net,www,
Examples of mismatches: 0,123,123www,


Only lowercase English characters can be entered

Expression: ^. [A-z]+$ For example
Description: Match English uppercase characters
Example of a match: ASP,CSDN
Examples of mismatches: 0,net,www,


Only English characters + digits can be entered

Expression: ^. [A-za-z0-9]+$ For example
Description: Match English characters + numbers
Examples of matches: 1ASP,W1W1W,
Examples of mismatches: 0,123,123,www,


Only English characters/numbers/underscores can be entered

Expression: ^\w+$ as an example
Description: Match English characters or numbers or underscores
Example of a match: 1asp,www,12,1_w
Examples of mismatches: 3#,2-4,w#$,


Password examples

Expression: ^. [A-za-z] \w{m,n}$
Description: Matches the m-n character at the beginning of an English character and can only be alphanumeric or underlined
Examples of matches
Examples of mismatches

Verify first letter Capitalization

Expression: \b[^\wa-z0-9_][^\wa-z0-9_]*\b

Description: First letter can only be capitalized
Example of a match: asp,net
Examples of mismatches:

Verify URL (with id= Chinese) vs.net2005 no this feature

Expression: ^http:\/\/([\w-]+ (\.[ \w-]+) + (\/[\w-. \/\?%&=\u4e00-\u9fa5]*)?)? $
Description: Validation band id= Chinese
Examples of matches: http://blog.csdn.net/?id= Qing yue er
Examples of mismatches:

Verifying Chinese characters

Expression: ^[\u4e00-\u9fa5]{0,}$
Description: Only Chinese characters
Example of a match: Qingqing Moon
Examples of mismatches:

Verify QQ number

Expression: [0-9]{5,9}
Description: QQ number of 5-9 digits
Example of a match: 10000,123456
Examples of mismatches: 10000w,


Verifying email (same as verifying MSN number)

Expression: \w+ ([-+.´]\w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *
Description: Note MSN with non-hotmail.com mailbox can also
Examples of matches: [email protected]
Examples of mismatches: [email protected].


Verify the ID number (rough, the best server-side tuning class library and further detailed verification)

Expression: ^[1-9] ([0-9]{16}|[ 0-9]{13}) [xx0-9]$
Describe:
Matching example: 15 or 18 digits of the ID number, supported with X
Examples of mismatches:

Verify the phone number (contains 159, does not contain PHS)

Expression: ^13[0-9]{1}[0-9]{8}|^15[9]{1}[0-9]{8}
Description: Contains 159 of the phone number 130-139
Example of matching: 139XXXXXXXX
Examples of mismatches: 140XXXXXXXX,


Verify the phone number (very complex, vs.net2005 is wrong)

Expression (imperfect): scheme one ((\ (\d{3}\) |\d{3}-) | ( \ (\d{4}\) |\d{4}-))? (\d{8}|\d{7})
Scenario Two (^[0-9]{3,4}\-[0-9]{3,8}$) | (^[0-9]{3,8}$) | (^\ ([0-9]{3,4}\) [0-9]{3,8}$] | (^0{0,1}13[0-9]{9}$) Support cell phone number but it's not perfect.
Describe:

Shanghai: 02112345678 3+8 bit
Shanghai: 021-12345678
Shanghai: (021)-12345678
Shanghai: (021) 12345678
Zhengzhou: 03711234567 4+7 bit
Hangzhou: 057112345678 4+8 bit
And the case with the extension number, the country code.
Because the situation is very complex so do not recommend the front desk to do 100% verification, so far it seems that no one can write a containing all types, in fact, there are many situations in itself is contradictory.
If anyone has a better verification call, please leave a message.
Examples of matches
Examples of mismatches


Verify your Passport

Expression: (p\d{7}) | G\D{8})
Description: Verify p+7 numbers and g+8 numbers
Examples of matches
Examples of mismatches

Verify IP

Expression: ^ (25[0-5]|2[0-4][0-9]|[ 0-1]{1}[0-9]{2}| [1-9] {1} [0-9] {1}| [1-9]) \. (25[0-5]|2[0-4][0-9]| [0-1] {1} [0-9] {2}| [1-9] {1} [0-9] {1}| [1-9]|0] \. (25[0-5]|2[0-4][0-9]| [0-1] {1} [0-9] {2}| [1-9] {1} [0-9] {1}| [1-9]|0] \. (25[0-5]|2[0-4][0-9]| [0-1] {1} [0-9] {2}| [1-9] {1} [0-9] {1}| [0-9]) $

Description: Verify IP
Example of a match: 192.168.0.1 222.234.1.4
Examples of mismatches


Verify Domain

Expression: ^[a-za-z0-9]+ ([a-za-z0-9\-\.] +)?\. (com|org|net|cn|com.cn|edu.cn|grv.cn|) $

Description: Authentication Domain
Example of a match: Csdn.net baidu.com it.com.cn
Examples of mismatches: 192.168.0.1


Verify credit card
Expression ^ ((?: 4\d{3}) | (? : 5[1-5]\d{2}) | (?: 6011) | (?: 3[68]\d{2}) | (?: 30[012345]\d)) [ -]? (\d{4}) [ -]? (\d{4}) [ -]? (\d{4}|3[4,7]\d{13}) $

Description Verify Visa Card, MasterCard, Discover Card, American Express card
Examples of matches
Examples of mismatches


Verifying ISBN International Standard ISBN

Expression: ^ (\d[-]*) {9}[\dxx]$

Description: Verifying ISBN International Standard ISBN
Example of a match: 7-111-19947-2
Examples of mismatches


Verify GUID Global Unique identifier

Expression: ^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$
Description: Format 8-4-4-4-12
Example of a match: 2064d355-c0b9-41d8-9ef7-9d8b26524751
Examples of mismatches


Verify file path and extension

Expression: ^ ([a-za-z]\:|\\) \ \ ([^\\]+\\) *[^\/:*? " <>|] +\.txt (l)? $
Description: Check the path and file name extension
Example of a match: E:\mo.txt
Examples of mismatches: E:\, Mo.doc, E:\mo.doc,


Validating HTML color values

Expression: ^#? ([a-f]| [a-f]| [0-9]) {3} ([a-f]| [a-f]| [0-9]) {3})? $
Description: Check color values
Examples of matches: #FF0000
Examples of mismatches

Several commonly used regular expressions [go]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.