C # Regular Expression summary

Source: Internet
Author: User

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

The following lists the various categories of characters, operators, and structures used to define regular expressions.

    • Character escapes
    • Character class
    • Anchor Point
    • Grouping constructs
    • Qualifier
    • Reverse reference Construction
    • Alternate construction
    • Replace
    • Miscellaneous constructs
Character escapes

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

The following table lists the escape characters:

Escape Character Description Mode Match
\a Matches the alarm (Bell) character \u0007. \a "\u0007" in "warning!" + ' \u0007 '
\b In the character class, match the backspace \u0008. [\b] {3,} "\b\b\b\b" in "\b\b\b\b"
\ t Matches the tab \u0009. (\w+) \ t "Name\t" and "addr\t" in "name\taddr\t"
\ r Matches the carriage return \u000d. (\ r is not equivalent to line break \ n.) ) \ r \ n (\w+) "\r\nhello" in "\r\hello\nworld."
\v Matches the Vertical tab \u000b. [\v] {2,} "\v\v\v" in "\v\v\v"
\f Matches the \u000c of the page break. [\f] {2,} "\f\f\f" in "\f\f\f"
\ n Matches the newline character \u000a. \ r \ n (\w+) "\r\nhello" in "\r\hello\nworld."
\e Matches the escape character \u001b. \e "\x001b" in "\x001b"
\ nnn Specifies a character using the octal representation (NNN consists of two to three digits). \w\040\w "A B" and "C D" in "a BC D"
\x nn Specifies the character using the hexadecimal representation (the NN happens to consist of two digits). \w\x20\w "A B" and "C D" in "a BC D"
\c x \c X Matches the x or x specified ASCII control character, where x or x is the letter of the control character. \cc "\x0003" in "\x0003" (ctrl-c)
\u nnnn Matches a Unicode character (four-digit number represented by nnnn) using the hexadecimal representation. \w\u0020\w "A B" and "C D" in "a BC D"
\ Matches the character when it is followed by an escaped character that does not recognize it. \d+[\+-x\*]\d+\d+[\+-x\*\d+ "3*9" and "3*9" in "(+/-*) *"
Character class

The character class matches any one character in a set of characters.

The following table lists the character classes:

character class Description Mode Match
[Character_group] Matches any single character in the Character_group. By default, matching is case-sensitive. [MN] "M" in "Mat", "M" and "N" in "Moon"
[^character_group] Non: matches 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: matches 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 character (. or \u002e), you must precede the character with an escape character (\.).
A.e "Ave" in "have", "ate" in "mate"
\p{name} Matches any single character in the Unicode generic category or named block specified by name . \p{lu} "C" and "L" in "City Lights"
\p{name} Matches any single character in a Unicode generic category or named block that is not specified in name . \p{lu} "I", "T" and "Y" in "City"
\w matches 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 whitespace character. \w\s "D" in "ID A1.3"
\s Matches any non-whitespace character. \s\s "_" in "int __ctr"
\d matches any decimal number. \d "4" in "4 = IV"
\d Matches any character that is not a decimal number. \d "", "=", "" "," I "and" V "in" 4 = IV "
Anchor Point

The anchor or atomic 0 width assertion causes the match to succeed or fail, depending on the current position in the string, but they do not cause the engine to advance or use characters in the string.

The following table lists the anchor points:

Assertion Description Mode Match
^ The match must start at the beginning of a string or a line. ^\D{3} "567" in "567-777-"
$ The match must appear at the end of the string or before \ n at the end of the row or string. -\d{4}$ "2012" in "8-12-2012"
\a The match must appear at the beginning of the string. \A\W{3} "Code" in "code-007-"
\z The match must appear at the end of the string or before \ n at the end of the string. -\d{3}\z "007" in "bond-901-007"
\z The match must appear at the end of the string. -\d{3}\z "-333" in "901-333"
\g The match must appear where the last match ended. \\G\ (\d\) "(1) (3) (5) [7] (9)" (1) "," (3) "and" (5) "
\b The match must appear on the boundary between \w(alphanumeric) and \w(non-alphanumeric) characters. \w "R", "O", "M" and "1" in "room#1"
\b The match must not appear on the \b boundary. \bend\w*\b "End sends endure lender" in "ends" and "Ender"
Grouping constructs

A grouping construct describes a subexpression of a regular expression, which is typically used to capture substrings of an input string.

The following table lists the grouping constructs:

Grouping Constructs Description Mode Match
(subexpression) Captures the matched sub-expression and assigns it to a zero-based ordinal. (\w) \1 "EE" in "deep"
(?< name >subexpression) Captures the matched sub-expression into a named group. (?< double>\w) \k< double> "EE" in "deep"
(?< name1-name2 >subexpression) Defines the balance group definition. (((?‘ Open ' \ () [^\ (\)]*) + ((? ') Close-open ' \)) [^\ (\)]*) +) * (? ( Open) (?!)) $ "((1-3) * (3-1)" In "3+2^ ((1-3) * (3-1))"
(?: subexpression) Defines a non-capturing group. Write (?: line)? "WriteLine" in "Console.WriteLine ()"
(? imnsx-imnsx:subexpression) Applies or disables the options specified in subexpression . A\d{2} (? i:\w+) \b "A12XL" and "A12XL" in "A12XL A12XL A12XL"
(? = subexpression) 0 width Positive lookahead assertion. \w+ (? =\.) "He is." The dog ran. "Is", "ran" and "out" in the sun are out.
(?! subexpression) 0 width Negative lookahead assertion. \b (?! UN) \w+\b "Sure" and "used" in "unsure sure unity used"
(?< =subexpression) 0 width is being recalled after the assertion is issued. (?< =19) \d{2}\b "2003" and "51" in "1851 1999 1950 1905 03"
(?<! subexpression) 0 width Negative review post assertion. (?<!19) \d{2}\b "End sends endure lender" in "ends" and "Ender"
(?> subexpression) Non-backtracking (also known as "greedy") sub-expressions. [13579] (? >a+b+) "1ABB", "3ABB" and "5AB" in "1ABB 3ABBC 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 for a match to occur. Qualifiers include the language elements listed in the following table.

The following table lists the qualifiers:

Qualifier Description Mode Match
* Matches the previous element 0 or more times. \d*\.\d ". 0", "19.9", "219.9"
+ Matches the previous element one or more times. "Be+" "Bee" in "Been", "be" in "bent"
? Matches the previous element 0 or one time. "Rai?n" "Ran", "Rain"
N Matches the previous element exactly n times. ", \d{3}" "1,043.6" in ", 043", "9,876,543,210" in ", 876", ", 543" and ", 210"
{N,} Matches the previous element at least n times. "\d{2,}" "166", "29", "1930"
{N, m} Matches the previous element at least n times, but not more than m times. "\d{3,5}" "166", "17668", "193024" in "19302"
*? Match the previous element 0 or more times, but as few times as possible. \d*?\.\d ". 0", "19.9", "219.9"
+? Matches the previous element one or more times, but as few times as possible. "Be+?" "Be" in "been", "be" in "bent"
?? Match the previous element 0 or one time, but as few times as possible. "Rai??" N "Ran", "Rain"
{n}? Matches the leading element exactly n times. ", \d{3}?" "1,043.6" in ", 043", "9,876,543,210" in ", 876", ", 543" and ", 210"
{N,}? Matches the previous element at least n times, but as few times as possible. "\d{2,}?" "166", "29" and "1930"
{n, m}? Matches the previous element between N and M, but as few times as possible. "\d{3,5}?" "166", "17668", "193024" in "193" and "024"
Reverse reference Construction

A reverse reference allows you to subsequently identify a previously matched subexpression in the same regular expression.

The following table lists the reverse reference constructs:

Reverse Reference Construction Description Mode Match
\ number Reverse reference. Matches the value of the number subexpression. (\w) \1 "EE" in "seek"
\k< name > Name the reverse reference. Matches the value of a named expression. (?< char>\w) \k< char> "EE" in "seek"
Alternate construction

Alternate constructs are used to modify regular expressions to enable either/or matching.

The following table lists the alternative constructs:

Alternate Construction Description Mode Match
| Matches any one element separated by a vertical bar (|) character. Th (E|is|at) "The" and "this" in
(? (expression) yes | NO) Matches Yesif the regular expression pattern is specified by expression match, otherwise matches the optional no part. 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) Matches Yesif name or named or numbered capturing group has a match, otherwise matches 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 replace pattern.

The following table lists the characters that are used for substitution:

character Description Mode Replacement Mode Input String result String
$Number Replaces substrings matched by group number. \b (\w+) (\s) (\w+) \b $3$2$1 "One" "One"
${name} Replaces a substring that matches the name of a named group. \b (?< word1>\w+) (\s) (?< word2>\w+) \b ${WORD2} ${word1} "One" "One"
$$ Replace the character "$". \b (\d+) \s? USD $$$1 "103 USD" "$103"
$& Replaces a copy of the entire match. (\$* (\d* (\.+\d+)?) {1}) **$& "$1.30" "**$1.30**"
$` Replaces all text of the input string before matching. B + $` "AABBCC" "AAAACC"
$ Replaces any text that matches the input string. B + $ "AABBCC" "AACCCC"
$+ Replace the last captured group. B + (c+) $+ "AABBCCDD" Aaccdd
$_ Replace the entire input string. B + $_ "AABBCC" "AAAABBCCCC"
Miscellaneous constructs

The following table lists various miscellaneous constructs:

Construction Description Example
(? imnsx-imnsx) Set or disable options such as case insensitivity in the middle of the pattern. \ba (? i) b\w+\b matches "ABA" and "Able" in "ABA Able Act"
(? #comment) Inline comments. The comment terminates at the first closing parenthesis. \ba (? #Matches words starting with A) \w+\b
[To end of line] X-mode comment. The comment begins with a non-escaped # and continues to the end of the line. (? x) \ba\w+\b#matches words starting with A
Regex class

The Regex class is used to represent a regular expression.

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

Ordinal Method & Description
1 public bool IsMatch (string INP UT)  
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 Regex Specifies whether the regular expression in the constructor finds a match in the specified input string, starting at the starting position specified in the string.
3 public static bool IsMatch (string input, string pattern)  
Indicates whether the specified regular expression finds a match in the specified input string.
4 public matchcollection Matches (string input)  
Search in the specified input string So Zheng all occurrences of an expression.
5 public string Replace (string input, string replacement)  
Replaces all matched strings that match the regular expression pattern with the specified replacement string in the specified input string.
6 public string[] Split (string input)  
Splits the input string into a substring array, based on the The Regex constructor specifies the location of the regular expression pattern definition to be split.

For a complete list of properties for the Regex class, see the 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

C # Regular Expression summary

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.