Detailed explanation of regular expression syntax

Source: Internet
Author: User
Tags control characters
Regular Expression.
1. Test the mode of a string. For example, you can test an input string to see if there is a phone number or a credit card number. This is called Data Validity verification.
2. Replace text. You can use a regular expression in a document to identify a specific text, and then delete it all or replace it with another text.
3. extract a substring from the string based on the pattern matching. It can be used to search for specific text in text or input fields.

 

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

 

Syntax Tag :/
Mark the next character as a special character, a literal character, or a backward reference, or an octal escape character. For example, 'n' matches the character "N ". '/N' matches a line break. The sequence '//' matches "/" and "/(" matches "(".

Syntax Tag: ^
Matches the start position of the input string. If the multiline attribute of the Regexp object is set, ^ matches the position after '/N' or'/R.
 
Syntax Tag: $
Matches the end position of the input string. If the multiline attribute of the Regexp object is set, $ also matches the location before '/N' or'/R.

 

Syntax Tag :*
Matches the previous subexpression zero or multiple times. For example, Zo * can match "Z" and "Zoo ". * Is equivalent to {0 ,}.

 

Syntax Tag: +

Match the previous subexpression once or multiple times. For example, 'Zo + 'can match "zo" and "Zoo", but cannot match "Z ". + Is equivalent to {1 ,}.

 

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

 

Syntax Tag: {n}
N is a non-negative integer. Match n times. For example, 'O {2} 'cannot match 'O' in "Bob", but can match two o in "food.

 

Syntax Tag: {n ,}
N is a non-negative integer. Match at least N times. For example, 'O {2,} 'cannot match 'O' in "Bob", but can match all o in "foooood. 'O {1,} 'is equivalent to 'o + '. 'O {0,} 'is equivalent to 'o *'.

 

Syntax Tag: {n, m}
Both m and n are non-negative integers, where n <= m. Match at least N times and at most m times. For example, "O {1, 3}" matches the first three o in "fooooood. 'O {0, 1} 'is equivalent to 'o? '. Note that there must be no space between a comma and two numbers.

 

Syntax Tag :?
When this character is followed by any other delimiter (*, + ,?, The matching mode after {n}, {n ,}, {n, m}) is not greedy. The non-Greedy mode matches as few searched strings as possible, while the default greedy mode matches as many searched strings as possible. For example, for strings "oooo", 'O ++? 'Will match a single "O", and 'O +' will match all 'O '.

 

Syntax Tag :.
Matches any single character except "/N. To match any character including '/N', use a pattern like' [./N.

Syntax Tag: (pattern)
Match pattern and obtain this match. The obtained match can be obtained from the generated matches set. The submatches set is used in VBScript, and $0… is used in JScript... $9 attribute. To match the parentheses, use '/(' or '/)'.
 
Syntax Tag :(? : Pattern)
Matches pattern but does not get the matching result. That is to say, this is a non-get match and is not stored for future use. This is useful when you use the "or" character (|) to combine each part of a pattern. For example, 'industr (? : Y | ies) is a simpler expression than 'industry | industries.

 

Syntax Tag :(? = Pattern)
Forward pre-query: matches the search string at the beginning of any string that matches the pattern. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows (? = 95 | 98 | nt | 2000) 'can match "Windows" in "Windows 2000", but cannot match "Windows" in "Windows 3.1 ". Pre-query does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.

 

Syntax Tag :(?! Pattern)
Negative pre-query: matches the search string at the beginning of any string that does not match pattern. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows (?! 95 | 98 | nt | 2000) 'can match "Windows" in "Windows 3.1", but cannot match "Windows" in "Windows 2000 ". Pre-query does not consume characters. That is to say, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.

 

Syntax Tag: X | y
Match X or Y. For example, 'z | food' can match "Z" or "food ". '(Z | f) Ood' matches "zood" or "food ".

 

Syntax Tag: [xyz]
Character Set combination. Match any character in it. For example, '[ABC]' can match 'A' in "plain '.

 

Syntax Tag: [^ XYZ]
Negative value character set combination. Match any character not included. For example, '[^ ABC]' can match 'p' in "plain '.

 

Syntax Tag: [A-Z]
Character range. Matches any character in the specified range. For example, '[A-Z]' can match any lowercase letter in the range of 'A' to 'Z.

 

Syntax Tag: [^ A-Z]
Negative character range. Matches any character that is not within the specified range. For example, '[^ A-Z]' can match any character that is not in the range of 'A' to 'Z.

 

Syntax Tag:/B
Match A Word boundary, that is, the position between a word and a space. For example, 'er/B 'can match 'er' in "never", but cannot match 'er 'in "verb '.

 

Syntax Tag:/B
Match non-word boundary. 'Er/B 'can match 'er' in "verb", but cannot match 'er 'in "never '.

 

Syntax Tag:/CX
Match the control characters specified by X. For example,/cm matches a control-M or carriage return character. The value of X must be either a A-Z or a-Z. Otherwise, C is treated as an original 'C' character.

 

Syntax Tag:/d
Match a numeric character. It is equivalent to [0-9].

 

Syntax Tag:/d
Match a non-numeric character. It is equivalent to [^ 0-9].

 

Syntax Tag:/F
Match a form feed. It is equivalent to/x0c and/Cl.

 

Syntax Tag:/n
Match A linefeed. It is equivalent to/x0a and/CJ.

 

Syntax Tag:/R
Match a carriage return. It is equivalent to/x0d and/cm.

 

Syntax Tag:/s
Matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to [/f/n/R/T/V].

 

Syntax Tag:/s
Match any non-blank characters. It is equivalent to [^/f/n/R/T/V].

 

Syntax Tag:/T
Match a tab. It is equivalent to/x09 and/CI.

 

Syntax Tag:/V
Match a vertical tab. It is equivalent to/x0b and/ck.

 

Syntax Tag:/W
Match any word characters that contain underscores. It is equivalent to '[A-Za-z0-9 _]'.

 

Syntax Tag:/W
Match any non-word characters. It is equivalent to '[^ A-Za-z0-9 _]'.

 

Syntax MARK:/XN
Match n, where N is the hexadecimal escape value. The hexadecimal escape value must be determined by the length of two numbers. For example, '/x41' matches "". '/X041' is equivalent to '/x04' & "1 ". The regular expression can use ASCII encoding ..

 

Syntax Tag:/num
Matches num, where num is a positive integer. References to the obtained matching. For example, '(.)/1' matches two consecutive identical characters.

 

Syntax Tag:/n
Identifies an octal escape value or a backward reference. If there are at least N obtained subexpressions before/N, then n is a backward reference. Otherwise, if n is an octal digit (0-7), n is an octal escape value.

 

Syntax Tag:/Nm
Identifies an octal escape value or a backward reference. If at least one child expression is obtained before/nm, the NM is backward referenced. If at least N records are obtained before/nm, n is a backward reference followed by text M. If none of the preceding conditions are met, if n and m are Octal numbers (0-7),/nm matches the octal escape value nm.

 

Syntax Tag:/NML
If n is an octal number (0-3) and M and l are Octal numbers (0-7), the octal escape value NML is matched.

Syntax Tag:/UN
Match n, where n is a Unicode character represented by four hexadecimal numbers. For example,/u00a9 matches the copyright symbol ().

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.