Basic summary of regular expressions

Source: Internet
Author: User

The basic syntax of regular expressions

Two special symbols ' ^ ' and ' $ '. Their role is to indicate the beginning and end of a string, respectively.

"^the": denotes all strings starting with "the" ("There", "the Cat", etc.);"of despair$": The string that endswith "of Despair"; "^abc$" : The start and end are "ABC" strings--hehe, only "abc" itself;"notice": Represents any string containing "notice".

If you do not use the ' ^ ' and ' $ ' two special characters, it means that the string you are looking for is in any part of the string you are looking for--not locating it at the top.

Others have ' * ', ' + ' and '? ' These three symbols represent the number of occurrences of a character or sequence of characters. They mean "no or more", "one or more" and "no or one more".

"ab*": Indicates that a string has an a followed by 0 or several B. ("A", "AB", "abbb",...) "ab+": Indicates that a string has an a followed by at least one B or more;"AB?" : Indicates that a string has a followed by 0 or a B;"a?b+$": indicates that there are 0 or one a followed by one or several B at the end of the string.

You can also use ranges, enclosed in curly braces, to represent the range of repetitions.

"Ab{2}": Indicates that a string has a followed by 2 B ("ABB");"Ab{2,}": Indicates that a string has a followed by at least 2 b;"ab{3,5}": A string that has a followed by 3 to 5 B.

' * ', ' + ' and '? ' Equivalent to "{0,}", "{1,}" and "{0,1}"

' ¦ ', means "or" Operation:

"Hi¦hello": Indicates a string with "hi" or "Hello","(B¦CD) EF": "bef" or "cdef";"(A¦b) *c": A string of "a" "B" mixed strings followed by a " C ";

‘.‘ Can replace any character:

"A.[0-9]": Indicates that a string has a "a" followed by an arbitrary character and a number;"^. {3}$ ": Represents any three-character string (3 characters long), and square brackets indicate that certain characters allow a specific position in a string to appear :" [AB] ": Indicates that a string has a" a "or" B "(equivalent to" a¦b " "[a-d]": Indicates a string containing a lowercase ' a ' to ' d ' one (equivalent to "a¦b¦c¦d" or "[ABCD]");"^[a-za-z]": A string that begins with a letter; "[0-9]%": A number that represents one digit before a percent semicolon;", [a-za-z0-9]$": Indicates that a string ends with a comma followed by a letter or a number.)

You can use ' ^ ' in square brackets to denote unwanted characters, ' ^ ' should be the first in square brackets. (For example: "%[^a-za-z]%" table
No letters should appear in the two percent sign).

In order to express it verbatim, you must be in "^.$ () ¦*+?" {\ ' These characters are preceded by the transfer character ' \ '.

Note that in square brackets, you do not need to escape characters.

Second, the application of the regular expression of an example of the description

1. Whether the check is all made up of numbers

/^[0-9]{1,20}$/^ indicates that the beginning of the character to match immediately after the following rule $ indicates that the beginning of the character to match immediately preceding the rule [] is an optional character set [0-9] that requires a character range of 0- between 9 {1,20} indicates that the numeric string length is valid from 1 to 20, that is, thenumber of occurrences of characters in [0-9] is 1 to 20 times. /^ and $/should be used to represent a rule that requires an exact match to be defined for the entire string, rather than just one substring in the string.

2. Verify the login name:

You can only enter 5-20 letters beginning with a letter, with numbers, "_", ". the string /^[a-za-z]{1} ([a-za-z0-9]|[. _]) {4,19}$/^[a-za-z]{1} indicates that the first character requirement is a letter. ([A-za-z0-9]| [._]) {4,19} represents a string from 4 to 19 bits in length starting from the second bit (since it immediately follows the previous expression), and it is required to consist of uppercase and lowercase letters, numbers, or special character set [. _].

3. Verify the user's name:

You can enter only 1-30 strings that start with a letter/^[a-za-z]{1,30}$/

4. Check the password:

You can enter only 6-20 letters, numbers, underscores/^ (\w) {6,20}$/\w: used to match letters, numbers, or underscore characters

5. Check the normal telephone, fax number:

can be "+" or the beginning of a number, can contain "-" and ""/^[+]{0,1} (\d) {1,3}[]? ([-]? ((\d) | []) {1,12}) +$/\d: Used to match numbers from 0 to 9; "? the metacharacters specify that the leading object must have 0 or more occurrences of a matched string in the target object such as:+123-999 999; +123-999 999; 123 999 999; +123 999999 etc.

6. Verifying URLs

/^http[s]{0,1}:\/\/.+$/or/^http[s]{0,1}:\/\/.{ 1,n}$/(indicates the length of the URL string ("https://") + N)/: denotes the character "/". Represents the set of all characters + equals {1,}, which is 1 to infinity.

7. Verifying Pure Chinese characters

/^[\u4e00-\u9fa5]+$/[\u4e00-\u9fa5]: estimated to be the range of Chinese character sets the above expressions are tested in the following JavaScriptfunctionREGX (R,s) {       if(r = =NULL|| r = = ""){              return false; }       varpatrn=NewRegExp (R); if(PATRN.exec(s))return true       return false} --></script><body><form>Rule expression:<input type= "Input" name= "Regxstr" value= "> (Fill in//between the expressions)<br>Check string:<input type= "input" name= "str" value= "" > <input type= "button" name= "Match" value= "match" onclick= "alert (regx (Regxstr.value,str.value)); " ></form></body>Three, the application of regular expression
"^\d+$"//non-negative integers (positive integers + 0)"^[0-9]*[1-9][0-9]*$"//positive integers"^ ((-\d+) | (0+)) $"//Non-positive integer (negative integer + 0)"^-[0-9]*[1-9][0-9]*$"//Negative integer"^-?\d+$"//integer"^\d+ (\.\d+)? $"//non-negative floating-point number (positive floating point + 0)^ ([0-9]+\]. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*)) $"//positive floating point number"^ ((-\d+ (\.\d+)?) | (0+ (\.0+)?)) $"//non-positive floating-point number (negative floating-point number + 0)^ (-([0-9]+\]. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*))) $"//Negative floating point number^ (-?\d+) (\.\d+)? $ "//floating point number"^[a-za-z]+$"//A string consisting of 26 English letters"^[a-z]+$"//A string consisting of 26 uppercase letters in English"^[a-z]+$"//A string of 26 English letters in lowercase"^[a-za-z0-9]+$"//A string consisting of a number and 26 English letters"^\w+$"//A string consisting of numbers, 26 letters, or underscores"^[\w-]+ (\.[ \w-]+) *@[\w-]+ (\.[ \w-]+) +$ "//Email Address"^[a-za-z]+://(\w+ (-\w+) *) (\. ( \w+ (-\w+) *) * (\?\s*)? $ "//URL/^ (D{2}|d{4})-((0 ([1-9]{1})) | ( 1[1|2])-(([0-2] ([1-9]{1})] | ( 3[0|1]) $///year-month-day/^ ((0 ([1-9]{1})) | (1[1|2])) /([0-2] ([1-9]{1}) | (3[0|1])) /(D{2}|d{4}) $///Month/day/year^ ([w.] +) @ ([[[0-9]{1,3}. [0-9] {1,3}. [0-9] {1,3}.) | ([w-]+.) +)) ([a-za-z]{2,4}| [0-9] {1,3}) (]?) $"//Emil"(d+-)?" (d{4}-?d{7}|d{3}-?d{8}|^d{7,8}) (-d+)? "//Phone number"^ (d{1,2}|1dd|2[0-4]d|25[0-5]). (D{1,2}|1dd|2[0-4]d|25[0-5]). (D{1,2}|1dd|2[0-4]d|25[0-5]). (D{1,2}|1dd|2[0-4]d|25[0-5]) $ "//IP Address^ ([0-9a-f]{2}) (-[0-9a-f]{2}) {5}$//regular Expressions for Mac addresses^[-+]?\d+ (\.\d+)? $//value-type regular expression

Basic summary of regular expressions

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.