Regular expression syntax-regular expression syntax

Source: Internet
Author: User
Tags alphabetic character

 

From http://msdn.microsoft.com/

A regular expression is a pattern of text that consists of ordinary characters (for example, letters A through Z) and special characters, knownMetacharacters. The pattern describes one or more strings to match when searching a body of text. The regular expression serves as a template for matching a character pattern to the string being searched.

Here are some examples of regular expression you might encounter:

JScript VBScript Matches
/^/S [/T] * $/ "^/S * $" Match a blank line.
// D {2}-/d {5 }/ "/D {2}-/d {5 }" Validate an ID number consisting of 2 digits, a hyphen, and another 5 digits.

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

Character Description
/ Marks the next character as either a special character, a literal, A backreference, or an octal escape. for example, 'n' matches the character "N ". '/N' matches a newline character. the sequence '// 'matches "/" and "/(" matches "(".
^ Matches the position at the beginning of the input string. IfRegexpObject'sMultilineProperty is set, ^ also matches the position following '/N' or'/R '.
$ Matches the position at the end of the input string. IfRegexpObject'sMultilineProperty is set, $ also matches the position preceding '/N' or'/R '.
* Matches the preceding character or subexpression zero or more times. For example, Zo * matches "Z" and "Zoo". * is equivalent to {0 ,}.
+ Matches the preceding character or subexpression one or more times. for example, 'Zo + 'matches "zo" and "Zoo", but not "Z ". + is equivalent to {1 ,}.
? Matches the preceding character or subexpression zero or one time. For example, "Do (ES )? "Matches the" do "in" do "or" does ".? Is equivalent to {0, 1}
{N} NIs a nonnegative integer. matches exactlyNTimes. For example, 'O {2} 'does not match the 'O' in "Bob," but matches the two's in "food ".
{N,} NIs a nonnegative integer. matches at leastNTimes. for example, 'O {2,} 'does not match the "O" in "Bob" and matches all the O's in "foooood ". 'O {1,} 'is equivalent to 'o + '. 'O {0,} 'is equivalent to 'o *'.
{N,M} MAndNAre nonnegative integers, whereN<=M. Matches at leastNAnd at mostMTimes. For example, "O {}" matches the first three o's in "fooooood". 'O {} 'is equivalent to' o? '. Note that you cannot put a space between the comma and the numbers.
? When this character immediately follows any of the other quantifiers (*, + ,?, {N},{N,},{N,M}), The matching pattern is non-greedy. A non-Greedy pattern matches as little of the searched string as possible, whereas the default greedy pattern matches as much of the searched string as possible. for example, in the string "oooo", 'O ++? 'Matches a single "O", while 'o + 'matches all 'O's.
. Matches any single character t "/N". To match any character including the '/N', use a pattern such as' [/S] '.
(Pattern) MatchesPatternAnd captures the match. The captured match can be retrieved from the resulting matches collection, usingSubmatchesCollection in VBScript or$0...$9Properties In JScript. To match parentheses characters (), use '/(' or '/)'.
(? :Pattern) MatchesPatternBut does not capture the match, that is, it is a non-capturing match that is not stored for possible later use. this is useful for combining Parts of a pattern with the "or" character (| ). for example, 'industr (? : Y | ies) is a more economical expression than 'industry | industries '.
(? =Pattern) Positive lookahead matches the search string at any point where a string matchingPatternBegins. This is a non-capturing match, that is, the match is not captured for possible later use. For example 'windows (? = 95 | 98 | nt | 2000) 'matches "Windows" in "Windows 2000" but not "Windows" in "Windows 3.1 ". lookaheads do not consume characters, that is, after a match occurs, the search for the next match begins immediately following the last match, not after the characters that comprised the lookahead.
(?!Pattern) Negative lookahead matches the search string at any point where a string not matchingPatternBegins. This is a non-capturing match, that is, the match is not captured for possible later use. For example 'windows (?! 95 | 98 | nt | 2000) 'matches "Windows" in "Windows 3.1" but does not match "Windows" in "Windows 2000 ". lookaheads do not consume characters, that is, after a match occurs, the search for the next match begins immediately following the last match, not after the characters that comprised the lookahead.
X|Y Matches eitherXOrY. For example, 'z | food 'matches "Z" or "food". '(z | f) Ood 'matches "zood" or "food ".
[XYZ] A character set. Matches any one of the enclosed characters. For example, '[ABC]' matches the 'A' in "plain ".
[^XYZ] A negative character set. Matches any character not enclosed. For example, '[^ ABC]' matches the 'p' in "plain ".
[A-z] A range of characters. Matches any character in the specified range. For example, '[A-Z]' matches any lowercase alphabetic character in the range 'a' through 'Z '.
[^A-z] A negative range characters. Matches any character not in the specified range. For example, '[^ A-Z]' matches any character not in the range 'a' through 'Z '.
/B Matches a word boundary, that is, the position between a word and a space. for example, 'er/B 'matches the 'er' in "never" but not the 'er 'in "verb ".
/B Matches a nonword boundary. 'er/B 'matches the 'er' in "verb" but not the 'er' in "never ".
/CX Matches the control character indicatedX. For example,/cm matches a control-M or carriage return character. The valueXMust be in the range of A-Z or a-Z. If not, C is assumed to be a literal 'C' character.
/D Matches a digit character. equivalent to [0-9].
/D Matches a nondigit character. equivalent to [^ 0-9].
/F Matches a form-feed character. equivalent to/x0c and/Cl.
/N Matches a newline character. equivalent to/x0a and/CJ.
/R Matches a carriage return character. equivalent to/x0d and/cm.
/S Matches any whitespace character including space, tab, form-feed, etc. equivalent to [/f/n/R/T/V].
/S Matches any non-white space character. equivalent to [^/f/n/R/T/V].
/T Matches a Tab character. equivalent to/x09 and/CI.
/V Matches a vertical Tab character. equivalent to/x0b and/ck.
/W Matches any word character including underscore. equivalent to '[A-Za-z0-9 _]'.
/W Matches any nonword character. equivalent to '[^ A-Za-z0-9 _]'.
/XN MatchesN, WhereNIs a hexadecimal escape value. hexadecimal escape values must be exactly two digits long. for example, '/x41' matches "". '/x041' is equivalent to '/x04' & "1 ". allows ASCII codes to be used in regular expressions.
/Num MatchesNum, WhereNumIs a positive integer. A reference back to captured matches. For example, '(.)/1' matches two consecutive identical characters.
/N Identifies either an octal escape value or a backreference. If/NIs preceded by at leastNCaptured subexpressions,NIs a backreference. Otherwise,NIs an octal escape value ifNIs an octal digit (0-7 ).
/Nm Identifies either an octal escape value or a backreference. If/NmIs preceded by at leastNmCaptured subexpressions,NmIs a backreference. If/NmIs preceded by at leastNCaptures,NIs a backreference followed by literalM. If neither of the preceding conditions exists ,/NmMatches octal escape ValueNmWhenNAndMAre octal digits (0-7 ).
/NML Matches octal escape ValueNMLWhenNIs an octal digit (0-3) andMAndLAre octal digits (0-7 ).
/UN MatchesN, WhereNIs a Unicode Character expressed as four hexadecimal digits. 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.