C # Regular Expression summary,

Source: Internet
Author: User

C # Regular Expression summary,

A regular expression is a pattern that matches input text .. . Net Framework provides a regular expression engine that allows such matching. A pattern consists of one or more characters, operators, and structures.

The characters, operators, and structures used to define regular expressions are listed below.

  • Character escape
  • Character class
  • Positioning point
  • Group Structure
  • Qualifier
  • Reverse reference construction
  • Standby Construction
  • Replace
  • Miscellaneous Construction
Character escape

The backslash (\) character in the regular expression indicates that it is followed by a special character or should be interpreted as an original character.

The following table lists escape characters:

Escape characters Description Mode Match
\ It matches the alarm (bell) character \ u0007. \ "Warning! "+" \ U0007 "in '\ u0007"
\ B In the character class, it matches the backspace key \ u0008. [\ B] {3 ,} "\ B" \ B"
\ T Match the tab \ u0009. (\ W +) \ t "Name \ t" and "Addr \ t" in "Name \ tAddr \ t"
\ R Match the carriage return \ u000D. (\ R is not equivalent to linefeed \ n .) \ R \ n (\ w +) "\ R \ nHello" in "\ r \ Hello \ nWorld"
\ V Match the vertical tab \ u000B. [\ V] {2 ,} "\ V" in "\ v"
\ F Match with the newline \ u000C. [\ F] {2 ,} "\ F" \ f"
\ N Match the linefeed \ u000A. \ R \ n (\ w +) "\ R \ nHello" in "\ r \ Hello \ nWorld"
\ E Match the Escape Character \ u001B. \ E "\ X001B" in "\ x001B"
\ Nnn Specify a character in octal notation (nnn consists of two to three digits ). \ W \ 040 \ w "A B" and "c d" in "a bc d"
\ X nn Specify characters in hexadecimal notation (nn exactly consists of two digits ). \ W \ x20 \ w "A B" and "c d" in "a bc d"
\ C X \ c x Matches the ASCII control characters specified by X or x, where X or x is the letter of the control character. \ CC "\ X0003" in "\ x0003" (Ctrl-C)
\ U nnnn Match a Unicode Character in hexadecimal notation (four digits represented by nnnn ). \ W \ u0020 \ w "A B" and "c d" in "a bc d"
\ This character is matched when it is followed by an unrecognized escape character. \ D + [\ +-x \ *] \ d + [\ +-x \ * \ d + "2 + 2" and "3*9" in "(2 + 2) * 3*9"
Character class

The character class matches any character in a group of characters.

The following table lists character classes:

Character class Description Mode Match
[Character_group] Match any single character in character_group. By default, matching is case sensitive. [Mn] "M" in "mat", "m" in "moon" and "n"
[^ Character_group] Non: match any single character that is not in character_group. By default, characters in character_group are case sensitive. [^ Aei] "V" and "l" in "avail"
[First-last] Character range: match any single character in the range from first to last. (\ W +) \ t "Name \ t" and "Addr \ t" in "Name \ tAddr \ t"
. Wildcard: matches any single character except \ n.
To match the original period (. Or \ u002E), you must add an escape character (\.) before the character (\.).
A.e "Ave" in "have", "ate" in "mate"
\ P {name} AndNameMatch any single character in the specified Unicode generic category or nameblock. \ P {Lu} "C" and "L" in "City Lights"
\ P {name} And not inNameMatch any single character in the specified Unicode generic category or nameblock. \ P {Lu} "I", "t", and "y" in "City"
\ W Match any word character. \ W "R", "o", "m", and "1" in "Room #1"
\ W Matches any non-word character. \ W "#" In "Room #1 "#"
\ S Matches any blank character. \ W \ s "D" in "ID A1.3"
\ S Matches any non-blank characters. \ S \ S "_" In "int _ ctr "_"
\ D Matches any decimal number. \ D "4" in "4 = IV"
\ D Match any character that is not in decimal number. \ D "4 = IV", "=", "", "I", and "V"
Positioning point

The positioning point or atomic zero-width assertion will make the match successful or fail, depending on the current position in the string, but they will not make the engine forward or use characters in the string.

The following table lists the positioning points:

Assertions Description Mode Match
^ The match must start with a string or a line. ^ \ D {3} "567" in "567-777"
$ Match must appear at the end of a string or at the end of a row or string\ NBefore. -\ D {4} $ -2012 in "8-12-2012"
\ The match must start with the string. \ A \ w {3} "Code" in "Code-007"
\ Z Match must appear at the end of the string or at the end of the string\ NBefore. -\ D {3} \ Z "-007" in "Bond-901-007"
\ Z The match must appear at the end of the string. -\ D {3} \ z "-901-333"-333"
\ G The match must appear at the end of the previous match. \ G \ (\ d \) "(1) (3) (5) [7] (9)" in "(1)", "(3)" and "(5 )"
\ B Matching must appear in\ W(Letters and numbers) and\ WThe boundary between (non-alphanumeric) characters. \ W "R", "o", "m", and "1" in "Room #1"
\ B Matching cannot appear in\ BBoundary. \ Bend \ w * \ B "Ends" and "ender" in "end sends endure lender"
Group Structure

The grouping structure describes the subexpression of the regular expression, which is usually used to capture the substring of the input string.

The following table lists the group structures:

Group Structure Description Mode Match
(Subexpression) Capture matched subexpressions and assign them to a sequence number starting from scratch. (\ W) \ 1 "Ee" in "deep"
(? <Name> subexpression) Capture matched subexpressions to a naming group. (? <Double> \ w) \ k <double> "Ee" in "deep"
(? <Name1-name2> subexpression) Define a balance group. (((? 'Open' \ () [^ \ (\)] *) + ((? 'Close-open' \) [^ \ (\)] *) + )*(? (Open )(?!)) $ "3 + 2 ^ (1-3) * (3-1)" (1-3) * (3-1 ))"
(? : Subexpression) Define a non-capturing group. Write (? : Line )? "WriteLine" in "Console. WriteLine"
(? Imnsx-imnsx: subexpression) Application or disabledSubexpression. A \ d {2 }(? I: \ w +) \ B "A12xl" and "A12XL" in "a12xl A12xl"
(? = Subexpression) 0-width positive prediction first asserted. \ W + (? = \.) "He is. The dog ran. The sun is out." in "is", "ran", and "out"
(?! Subexpression) 0-width negative prediction first asserted. \ B (?! Un) \ w + \ B "Sure" and "used" in "unsure sure unity used"
(? <= Subexpression) Assertion after the blank width is reviewed. (? <= 19) \ d {2} \ B "1851 1999 1950 1905 2003" in "51" and "03"
(? <! Subexpression) Assertion after review with Zero Width and negative. (? <! 19) \ d {2} \ B "Ends" and "ender" in "end sends endure lender"
(?> Subexpression) A non-backtracking (also called "greedy") subexpression. [13579] (?> A + B +) "1ABB", "3ABB", and "5AB" in "1ABB 3 ABBC 5AB 5AC"
Qualifier

The qualifier specifies how many instances of the previous element (which can be a character, group, or character class) must exist in the input string to match. The qualifier includes the language elements listed in the following table.

The following table lists the delimiters:

Qualifier Description Mode Match
* Matches the previous element zero or multiple times. \ D * \. \ d ". 0", "19.9", "219.9"
+ Match the previous element once or multiple times. "Be +" "Be" in "bee", "bent" in "been"
? Matches the previous element zero times or once. "Rai? N" "Ran", "rain"
{N} Match the previous element EXACTLY n times. ", \ D {3 }" "1,043.6", 043 "," 9,876,543,210 ", 876", ", 543", and ", 210"
{N ,} Match the previous element at least n times. "\ D {2 ,}" "166", "29", "1930"
{N, m} Match the previous element at least n times, but not more than m times. "\ D {3, 5 }" "166", "17668", "193024" in "19302"
*? Matches the previous element zero or multiple times, but the number of times is as few as possible. \ D *? \. \ D ". 0", "19.9", "219.9"
+? Match the previous element once or multiple times, but the number of times is as small as possible. "Be +? " "Be" in "been", "be" in "bent"
?? Matches the previous element zero or once, but the number of times is as small as possible. "Rai ?? N" "Ran", "rain"
{N }? Match the leading element EXACTLY n times. ", \ D {3 }? " "1,043.6", 043 "," 9,876,543,210 ", 876", ", 543", and ", 210"
{N ,}? Match the previous element at least n times, but the number of times is as few as possible. "\ D {2 ,}? " "166", "29" and "1930"
{N, m }? The number of times the last element is matched is between n and m, but the number of times is as small as possible. "\ D {3, 5 }? " "166", "17668", "193024" and "024"
Reverse reference construction

Reverse reference allows you to subsequently identify the previously matched child expressions in the same regular expression.

The following table lists reverse reference structures:

Reverse reference construction Description Mode Match
\ Number Reverse reference. Matches the value of the number subexpression. (\ W) \ 1 "Ee" in "seek"
\ K <name> Name reverse reference. Matches the value of a naming expression. (? <Char> \ w) \ k <char> "Ee" in "seek"
Standby Construction

The secondary structure is used to modify the regular expression to enable either/or matching.

The following table lists the standby structures:

Standby Construction Description Mode Match
| Matches any element separated by a vertical line (|. Th (e | is |) "This is the day." in "the" and "this"
(? (Expression) yes | no) If the regular expression mode is specified by expression matchingYes; Otherwise, the matching is optional.No. Expression is interpreted as a zero-width assertion. (? (A) A \ d {2} \ B | \ B \ d {3} \ B) "A10" and "910" in "A10 C103 910"
(? (Name) yes | no) If the name or named or numbered capture group matchesYes; Otherwise, the matching is optional.No. (? <Quoted> ")? (? (Quoted). +? "| \ S + \ s) "Dogs.jpg" Yiska playing.jpg "" Dogs.jpg and "Yiska playing.jpg"
Replace

Replace is the regular expression used in the replacement mode.

The following table lists the characters used for replacement:

Character Description Mode Replacement Mode Input string Result string
$Number Replace by groupNumberMatched substring. \ B (\ w +) (\ s) (\ w +) \ B $3 $2 $1 "One two" "Two one"
$ {Name} Replace named groupsNameMatched substring. \ B (? <Word1> \ w +) (\ s )(? <Word2> \ w +) \ B ${Word2 }$ {word1} "One two" "Two one"
$ Replace the character "$ ". \ B (\ d +) \ s? USD $1 "103 USD" "$103"
$ & Replace a copy of the entire matching item. (\ $ * (\ D * (\. + \ d + )?) {1 }) ** $ & "$1.30" "** $1.30 **"
$' Replace all text of the input string before matching. B + $' "AABBCC" "AAAACC"
$' Replace all text of the matched input string. B + $' "AABBCC" "AACCCC"
$ + Replace the last captured group. B + (C +) $ + "AABBCCDD" AACCDD
$ _ Replace the entire input string. B + $ _ "AABBCC" "AAAABBCCCC"
Miscellaneous Construction

The following table lists various miscellaneous structures:

Structure Description Instance
(? Imnsx-imnsx) Set or disable options such as case-insensitive in mode. \ BA (? I) B \ w + \ B matches "ABA" and "Able" in "ABA Able Act"
(? # Comment) Inline comments. The comment ends at the first right brace. \ BA (? # Matches words starting with A) \ w + \ B
#[To end of line] X mode annotation. The comment starts with a non-escape # And continues to the end of the row. (? X) \ bA \ w + \ B # Matches words starting with
Regex class

The Regex class is used to represent a regular expression.

The following table lists some common methods in the Regex class:

Serial number Method & Description
1 Public bool IsMatch (string input)
Indicates whether the regular expression specified in the Regex constructor finds a match in the specified input string.
2 Public bool IsMatch (string input, int startat)
Indicates whether the regular expression specified in the Regex constructor finds a match in the specified input string, starting from the start position specified in the string.
3 Public static bool IsMatch (string input, string pattern)
Indicates whether the specified Regular Expression matches the specified input string.
4 Public MatchCollection Matches (string input)
Search for all matching items of the regular expression in the specified input string.
5 Public string Replace (string input, string replacement)
In the specified input string, replace all matching strings that match the regular expression pattern with the specified replacement string.
6 Public string [] Split (string input)
Splits the input string into a substring Array Based on the location defined in the regular expression mode specified in the Regex constructor.

For a complete list of properties for the Regex class, see Microsoft C # documentation: https://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex (v = vs.110). aspx.

Source: http://www.runoob.com/csharp/csharp-regular-expressions.html

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.