The meaning of some regular expressions

Source: Internet
Author: User

Regular expressions

1. Meaning of regular expressions

\d matches a numeric character,/\d/=/[0-9]/

\d matches a non-numeric character,/\d/=/[^0-9]/

\s matches a white space character, including \n,\r,\f,\t,\v , etc.

\s matches a non-whitespace character equal to /[^\n\f\r\t\v]/

\w matches a character that can make up a word, including underscores

\w matches a character that cannot be composed of words, equal to [^a-za-z0-9].

\ t matches a tab

\ n matches a line break

\ r matches a carriage return character

\ As a turn, the character usually after "\" is not interpreted as the original meaning, as \* represents an * number

^ matches the beginning of an input or a line,/^a/ matches "an A", and does not match "an A"

$ matches the end of an input or a line,/a$/ matches "an A", and does not match "an A"

* match the preceding metacharacters 0 or more times,/ba*/ will match B,ba,baa,baaa

+ match Front metacharacters 1 or more times,/ba+/ will match Ba,baa,baaa

? Match Front metacharacters 0 Times or 1 Times, /ba?/ will match B,ba

{n} exact match n times

{N,} Match N Times above

{N,m} Match n-m Times

. any character other than a line break , equivalent to [^\n]

X|y match x or y

[XYZ] Character Set (character Set) to match any of the one by one characters in this set ( or meta-character )

[^XYZ] does not match any one of the characters in this collection

[\b] match a backspace

2, using the syntax of the regular table above , You can describe the two-digit /\ d \ d/, the four-digit number described as /\d \ d \ d \ d/

3. Example

/\d{2, 4}/matches numbers between 2 and 4 .

/\W{3} \d?/matches three single character and an arbitrary number .

/\s+java\s+/matches the string "Java", and it can have one or more spaces before and after it .

4, commonly used regular expressions ( Some regular expressions are not very accurate, when using the self-modification can be )

1.match The regular expression of email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *

2. Regular expressionmatching url url :[a-za-z]+://[^\s]*

3. Match domestic phone number: \d{3}-\d{8}|\d{4}-\d{7}

\D{3}-\D{8}|\D{4}-\D{7}

Commentary: Match forms such as 0511-4405222 or 021-87888822

4. Matching ID:\d{15}|\d{18}

5. Match IP address:\d+\.\d+\.\d+\.\d+

6, match a specific number:
^[1-9]\d*$    //match positive integers
^-[1-9]\d*$  //match negative integers
^-? [1-9]\d*$   //Match integer
^[1-9]\d*|0$  //matches a non-negative integer (positive integer+ 0)
^-[1-9]\d*|0$   //match a non-positive integer (negative integer+ 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$   //match Positive floating point number
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*) $  //match negative floating-point numbers
^-? ([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0) $  //matching floating-point numbers
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$   //matching nonnegative floating-point numbers (positive floating-point numbers )+ 0)
^ (-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)) |0?\.0+|0$  //matching non-positive floating-point numbers (negative floating-point numbers )+ 0)
Commentary: useful when dealing with large amounts of data, with particular attention to remediation when applied

7. Match a specific string:
^[a-za-z]+$//Match a string consisting of 26 English letters
^[a-z]+$//matches a string consisting of 26 uppercase letters
^[a-z]+$//matches a string consisting of 26 letters in lowercase
^[a-za-z0-9]+$//matches a string consisting of a number and 26 English letters
^\w+$//matches a string consisting of a number, 26 letters, or underscores

To verify the Chinese regular expression:

/^ ([\u4e00-\u9fa5]| [\ufe30-\uffa0]) *$/

Whether the checksum is all made up of numbers
var patrn=/^[0-9]{1,20}$/

Check login name: Only 5-20 entries begin with a letter, can be numbered, "_", "." The string
var patrn=/^[a-za-z]{1} ([a-za-z0-9]|[. _]) {4,19}$/;

Verify user name: Only 1-30 strings beginning with a letter can be entered
var patrn=/^[a-za-z]{1,30}$/;

Check password: Only 6-20 letters, numbers, underscores can be entered
var patrn=/^ (\w) {6,20}$/;

Check the ordinary telephone, fax number: Can "+" start, in addition to the number, can contain "-"
var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? (\d) {1,12}) +$/;
var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? ((\d) | []) {1,12}) +$/;

Check mobile phone Number: Must start with a number, except the number, can contain "-"
var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? ((\d) | []) {1,12}) +$/;

Verifying ZIP Codes
var patrn=/^[a-za-z0-9]{3,12}$/;

Verifying search Keywords
var patrn=/^[^ ' [email protected]#$%^&* () +=|\\\][\]\{\}:; ' \,.<>/?] {1} [^ ' [email protected]$%^& () +=|\\\] [\]\{\}:;‘ \,.<>?] {0,19}$/;
var patrn=/^[0-9.] {1,20}$/;

Regular expressions
^\\d+$//nonnegative integers (positive integers + 0)
^[0-9]*[1-9][0-9]*$//Positive integer
^ ((-\\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 number + 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]+$//String consisting of 26 English letters in lowercase
^[a-za-z0-9]+$//String consisting of a number and 26 English letters
^\\w+$//String consisting of numbers, 26 English letters or underscores
^[\\w-]+ (\\.[ \\w-]+) *@[\\w-]+ (\\.[ \\w-]+) +$//email Address
^[a-za-z]+://(\\w+ (-\\w+) *) (\ \. ( \\w+ (-\\w+) *) * (\\?\\s*)? $//url
^[a-za-z0-9_]*$ JS Regular expression

Ghost posted in [2008-01-17 23:17]

Whether the checksum is all made up of numbers
var patrn=/^[0-9]{1,20}$/

Check login name: Only 5-20 entries begin with a letter, can be numbered, "_", "." The string
var patrn=/^[a-za-z]{1} ([a-za-z0-9]|[. _]) {4,19}$/;

Verify user name: Only 1-30 strings beginning with a letter can be entered
var patrn=/^[a-za-z]{1,30}$/;

Check password: Only 6-20 letters, numbers, underscores can be entered
var patrn=/^ (\w) {6,20}$/;

Check the ordinary telephone, fax number: Can "+" start, in addition to the number, can contain "-"
var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? (\d) {1,12}) +$/;
var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? ((\d) | []) {1,12}) +$/;

Check mobile phone Number: Must start with a number, except the number, can contain "-"
var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? ((\d) | []) {1,12}) +$/;

Verifying ZIP Codes
var patrn=/^[a-za-z0-9]{3,12}$/;

Verifying search Keywords
var patrn=/^[^ ' [email protected]#$%^&* () +=|\\\][\]\{\}:; ' \,.<>/?] {1} [^ ' [email protected]$%^& () +=|\\\] [\]\{\}:;‘ \,.<>?] {0,19}$/;
var patrn=/^[0-9.] {1,20}$/;

Regular expressions
^\\d+$//nonnegative integers (positive integers + 0)
^[0-9]*[1-9][0-9]*$//Positive integer
^ ((-\\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 number + 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]+$//String consisting of 26 English letters in lowercase
^[a-za-z0-9]+$//String consisting of a number and 26 English letters
^\\w+$//String consisting of numbers, 26 English letters or underscores
^[\\w-]+ (\\.[ \\w-]+) *@[\\w-]+ (\\.[ \\w-]+) +$//email Address
^[a-za-z]+://(\\w+ (-\\w+) *) (\ \. ( \\w+ (-\\w+) *) * (\\?\\s*)? $//url
^[a-za-z0-9_]*$

The meaning of some 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.