Regular Expressions (also known as regular expressions)

Source: Internet
Author: User

A regular expression is a text pattern that includes common characters (for example, letters between A and Z) and special characters (called"Metacharacters"). Mode description one or more strings to be matched when searching text.

Regular Expression example
Expression Match

/^ \ S * $/

Matches empty rows.

/\ D {2}-\ D {5 }/

The ID number consists of two digits, one hyphen and five digits.

/<\ S * (\ s +) (\ s [^>] *)?> [\ S] * <\ s * \/\ 1 \ s *>/

Matches HTML tags.

The following table contains a complete list of metacharacters and their behavior in the context of a regular expression:

 
Character Description

\

Mark the next character as a special character, text, reverse reference, or octal escape character. For example, "N" matches the character "N ". "\ N" matches the line break. The sequence "\" matches "\", and "\ (" matches "(".

^

Match the start position of the input string. IfRegexpObjectMultilineProperty, ^ also matches the position after "\ n" or "\ r.

$

Matches the position at the end of the input string. IfRegexpObjectMultilineAttribute, $ also matches the position before "\ n" or "\ r.

*

Matches the previous character or subexpression zero or multiple times. For example, Zo * matches "Z" and "Zoo ". * Is equivalent to {0 ,}.

+

Match the previous character or subexpression one or more times. For example, "zo +" matches "zo" and "Zoo", but does not match "Z. + Is equivalent to {1 ,}.

?

Matches the previous character or subexpression zero or once. For example, "Do (ES )?" Match "do" in "do" or "does ".? It is equivalent to {0, 1 }.

{N}

NIt is a non-negative integer. Exactly matchNTimes. For example, "o {2}" does not match "O" in "Bob", but matches two "O" in "food.

{N,}

NIt is a non-negative integer. At least matchNTimes. For example, "o {2,}" does not match "O" in "Bob", but matches all o in "foooood. "O {1,}" is equivalent to "O + ". "O {0,}" is equivalent to "O *".

{N,M}

MAndNIs a non-negative integer.N<=M. Match at leastNTimes, upMTimes. For example, "O {1, 3}" matches the first three o in "fooooood. 'O {0, 1} 'is equivalent to 'o? '. Note: you cannot insert spaces between commas and numbers.

?

When this character is followed by any other qualifier (*, + ,? ,{N},{N,},{N,M}), The matching mode is "not greedy ". The "non-greedy" Mode matches the searched strings as short as possible, while the default "greedy" Mode matches the searched strings as long as possible. For example, in the string "oooo", "O + ?" Only one "O" is matched, and "O +" is matched with all "O ".

.

Match any single character except "\ n. To match any character including "\ n", use a mode such as "[\ s.

(Pattern)

MatchPatternAnd capture the matched child expression. Available$0... $9The property is retrieved from the "match" set of the result. To match the parentheses (), use "\ (" or "\)".

(? :Pattern)

MatchPatternHowever, the child expression that does not capture the match, that is, it is a non-capturing match and is not stored for future use. This is useful for components that use the "or" character (|) combination mode. For example, 'industr (? : Y | ies) is a more economical expression than 'industry | industries.

(? =Pattern)

Execute the subexpression of Forward prediction first search, which matchesPatternThe start point of the string. It is a non-capture match, that is, it cannot be captured for future use. For example, 'windows (? = 95 | 98 | nt | 2000) 'matches "Windows" in "Windows 2000", but does not match "Windows" in "Windows 3.1 ". Prediction first does not occupy characters, that is, after a match occurs, the next matched search follows the previous match, rather than after the characters that constitute prediction first.

(?!Pattern)

Execute the subexpression of the reverse prediction first search. This expression does not matchPatternThe start point of the string. It is a non-capture match, that is, it cannot be captured for future use. For example, 'windows (?! 95 | 98 | nt | 2000) 'matches "Windows" in "Windows 3.1", but does not match "Windows" in "Windows 2000 ". Prediction first does not occupy characters, that is, after a match occurs, the next matched search follows the previous match, rather than after the characters that constitute prediction first.

X|Y

MatchXOrY. For example, 'z | food' matches "Z" or "food ". '(Z | f) Ood' matches "zood" or "food ".

[XYZ]

Character Set. Match any character. For example, "[ABC]" matches "A" in "plain ".

[^XYZ]

Reverse character set. Match any character that is not included. For example, "[^ ABC]" matches "P" in "plain ".

[A-z]

Character range. Matches any character in the specified range. For example, "[A-Z]" matches any lowercase letter in the range of "A" to "Z.

[^A-z]

Reverse range character. Matches any character that is not within the specified range. For example, "[^ A-Z]" matches any character that is not in the range of "A" to "Z.

\ B

Match A Word boundary, that is, the position between the word and the space. For example, "Er \ B" matches "er" in "never", but does not match "er" in "verb ".

\ B

Non-word boundary match. "Er \ B" matches "er" in "verb", but does not match "er" in "never ".

\ CX

MatchXIndicates the control character. For example, \ cm matches control-M or carriage return.XMust be between the A-Z or a-Z. If this is not the case, it is assumed that C is the "c" character itself.

\ D

Match numeric characters. It is equivalent to [0-9].

\ D

Match non-numeric characters. It is equivalent to [^ 0-9].

\ F

Match the page feed. It is equivalent to \ x0c and \ Cl.

\ N

Line feed match. It is equivalent to \ x0a and \ CJ.

\ R

Match a carriage return. It is equivalent to \ x0d and \ cm.

\ S

Matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to [\ f \ n \ r \ t \ v.

\ S

Match any non-blank characters. It is equivalent to [^ \ f \ n \ r \ t \ v.

\ T

Tab matching. It is equivalent to \ x09 and \ CI.

\ V

Vertical tab matching. It is equivalent to \ x0b and \ ck.

\ W

Matches any character type, including underscores. Equivalent to [A-Za-z0-9.

\ W

Matches any non-word character. Equivalent to [^ A-Za-z0-9.

\ XN

MatchN,NIs a hexadecimal escape code. The hexadecimal escape code must be exactly two digits long. For example, "\ x41" matches "". "\ X041" is equivalent to "\ x04" & "1. ASCII code can be used in regular expressions.

\Num

MatchNum,NumIs a positive integer. To capture matched reverse references. For example, "(.) \ 1" matches two consecutive identical characters.

\N

Identifies an octal escape code or a reverse reference. If \NAt leastNCapture sub-expressions, thenNIs a reverse reference. Otherwise, ifNIs the eight-digit number (0-7), thenNIt is an octal escape code.

\Nm

Identifies an octal escape code or a reverse reference. If \NmAt leastNmCapture sub-expressions, thenNmIs a reverse reference. If \NmAt leastNCaptureNIs reverse reference, followed by charactersM. If neither of the preceding conditions exists, then \NmMatching octal valuesNm, WhereNAndMIt is an octal digit (0-7 ).

\ NML

WhenNIs the number of octal (0-3 ),MAndLMatch the octal escape code when it is an octal number (0-7 ).NML.

\ UN

MatchN, WhereNIt is a Unicode Character in hexadecimal notation. For example, \ u00a9 matches the copyright symbol (©).

Regular Expressions (also known as 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.