# 規則運算式語言 - 快速參考Regular Expression Language - Quick Reference

## 逸出字元Character Escapes

`\a` 比對警示字元 \u0007。Matches a bell character, \u0007. `\a` "Error!" 中的 "\u0007""\u0007" in "Error!" + '\u0007'+ '\u0007'
`\b` 在字元類別中，比對退格鍵 \u0008。In a character class, matches a backspace, \u0008. `[\b]{3,}` "\b\b\b\b" 中的 "\b\b\b\b""\b\b\b\b" in "\b\b\b\b"
`\t` 比對定位點 \u0009。Matches a tab, \u0009. `(\w+)\t` "item1\titem2\t" 中的 "item1\t"、"item2\t""item1\t", "item2\t" in "item1\titem2\t"
`\r` 比對歸位字元 \u000DMatches a carriage return, \u000D. (`\r` 不等於新行字元 `\n`。)(`\r` is not equivalent to the newline character, `\n`.) `\r\n(\w+)` "\r\nThese are\ntwo lines." 中的 "\r\nThese""\r\nThese" in "\r\nThese are\ntwo lines."
`\v` 比對垂直定位點 \u000B。Matches a vertical tab, \u000B. `[\v]{2,}` "\v\v\v" 中的 "\v\v\v""\v\v\v" in "\v\v\v"
`\f` 比對換頁字元 \u000C。Matches a form feed, \u000C. `[\f]{2,}` "\f\f\f" 中的 "\f\f\f""\f\f\f" in "\f\f\f"
`\n` 比對新行字元 \u000A。Matches a new line, \u000A. `\r\n(\w+)` "\r\nThese are\ntwo lines." 中的 "\r\nThese""\r\nThese" in "\r\nThese are\ntwo lines."
`\e` 比對逸出字元 \u001B。Matches an escape, \u001B. `\e` "\x001B" 中的 "\x001B""\x001B" in "\x001B"
`\` nnn`\` nnn 使用八進位表示法指定字元 (nnn 由兩位數或三位數組成)。Uses octal representation to specify a character (nnn consists of two or three digits). `\w\040\w` "a b", "c d" in"a b", "c d" in

"a bc d""a bc d"
`\x` nn`\x` nn 使用十六進位表示指定字元 (nn 由剛好兩位數組成)。Uses hexadecimal representation to specify a character (nn consists of exactly two digits). `\w\x20\w` "a b", "c d" in"a b", "c d" in

"a bc d""a bc d"
`\c` X`\c` X

`\c` x`\c` x

`\u` nnnn`\u` nnnn 使用十六進位表示比對 Unicode 字元 (剛好四位數，如 nnnn所表示)。Matches a Unicode character by using hexadecimal representation (exactly four digits, as represented by nnnn). `\w\u0020\w` "a b", "c d" in"a b", "c d" in

"a bc d""a bc d"
`\` 當後面加上的字元不是這張表和本主題其他表格中指出的逸出字元時，則比對該字元。When followed by a character that is not recognized as an escaped character in this and other tables in this topic, matches that character. 例如， `\*``\x2A`相同，而 `\.``\x2E`相同。For example, `\*` is the same as `\x2A`, and `\.` is the same as `\x2E`. 這可讓規則運算式引擎釐清語言元素 (例如 * 或 ?) 和字元常值 (以 `\*``\?` 表示)。This allows the regular expression engine to disambiguate language elements (such as * or ?) and character literals (represented by `\*` or `\?`). `\d+[\+-x\*]\d+` "(2+2) * 3*9" 中的 "2+2" 和 "3*9""2+2" and "3*9" in "(2+2) * 3*9"

## 字元類別Character Classes

`[` character_group `]``[` character_group `]` 比對 character_group中的任何單一字元。Matches any single character in character_group. 根據預設，比對會區分大小寫。By default, the match is case-sensitive. `[ae]` "gray" 中的 "a""a" in "gray"

"lane" 中的 "a"、"e""a", "e" in "lane"
`[^` character_group `]``[^` character_group `]` 否定：比對不在 character_group中的任何單一字元。Negation: Matches any single character that is not in character_group. 根據預設， character_group 中的字元會區分大小寫。By default, characters in character_group are case-sensitive. `[^aei]` "reign" 中的 "r"、"g"、"n""r", "g", "n" in "reign"
`[` 第一 `-` last `]``[` first `-` last `]` 字元範圍：比對從 firstlast範圍內的任何單一字元。Character range: Matches any single character in the range from first to last. `[A-Z]` "AB123" 中的 "A"、"B""A", "B" in "AB123"
`.` 萬用字元：比對除 \n 以外的任何單一字元。Wildcard: Matches any single character except \n.

`a.e` "nave" 中的 "ave""ave" in "nave"

"water" 中的 "ate""ate" in "water"
`\p{` name `}``\p{` name `}` 比對 Unicode 一般分類中或 name指定之具名區塊中的任何單一字元。Matches any single character in the Unicode general category or named block specified by name. `\p{Lu}`

`\p{IsCyrillic}`
"City Lights" 中的 "C"、"L""C", "L" in "City Lights"

"ДЖem" 中的 "Д"、"Ж""Д", "Ж" in "ДЖem"
`\P{` name `}``\P{` name `}` 比對不在 Unicode 一般分類中或 name所指定之具名區塊中的任何單一字元。Matches any single character that is not in the Unicode general category or named block specified by name. `\P{Lu}`

`\P{IsCyrillic}`
"City" 中的 "i"、"t"、"y""i", "t", "y" in "City"

"ДЖem" 中的 "e"、"m""e", "m" in "ДЖem"
`\w` 比對任何文字字元。Matches any word character. `\w` "ID A1.3" 中的 "I"、"D"、"A"、"1"、"3""I", "D", "A", "1", "3" in "ID A1.3"
`\W` 比對任何非文字字元。Matches any non-word character. `\W` "ID A1.3" 中的 " "、"."" ", "." in "ID A1.3"
`\s` 比對任何泛空白字元。Matches any white-space character. `\w\s` "ID A1.3" 中的 "D ""D " in "ID A1.3"
`\S` 比對任何非泛空白字元。Matches any non-white-space character. `\s\S` "int _ctr" 中的 " "" _" in "int __ctr"
`\d` 比對任何十進位數字。Matches any decimal digit. `\d` "4 = IV" 中的 "4""4" in "4 = IV"
`\D` 比對十進位數字以外的任何字元。Matches any character other than a decimal digit. `\D` "4 = IV" 中的 " "、"="、" "、"I"、"V"" ", "=", " ", "I", "V" in "4 = IV"

## 錨點Anchors

`^` 根據預設，比對必須在字串的開頭開始；在多行模式中，它必須在一行的開頭開始。By default, the match must start at the beginning of the string; in multiline mode, it must start at the beginning of the line. `^\d{3}` "901-333-" 中的"901" in

"901-333-""901-333-"
`\$` 根據預設，比對必須發生在字串的結尾或字串結尾的 `\n` 之前；在多行模式中，它必須發生在一行的結尾之前，或一行結尾的 `\n` 之前。By default, the match must occur at the end of the string or before `\n` at the end of the string; in multiline mode, it must occur before the end of the line or before `\n` at the end of the line. `-\d{3}\$` "-333-333-" 中的"-333" in

"-333""-901-333"
`\A` 比對必須發生在字串開頭。The match must occur at the start of the string. `\A\d{3}` "901-333-" 中的"901" in

"901-333-""901-333-"
`\Z` 比對必須發生在字串結尾，或發生在字串結尾的 `\n` 之前。The match must occur at the end of the string or before `\n` at the end of the string. `-\d{3}\Z` "-333-333-" 中的"-333" in

"-333""-901-333"
`\z` 比對必須發生在字串結尾。The match must occur at the end of the string. `-\d{3}\z` "-333-333-" 中的"-333" in

"-333""-901-333"
`\G` 比對必須發生在先前比對結束的位置。The match must occur at the point where the previous match ended. `\G\(\d\)` "(1)(3)(5)[7](9)" 中的 "(1)"、"(3)"、"(5)""(1)", "(3)", "(5)" in "(1)(3)(5)[7](9)"
`\b` 比對必須發生在 `\w` (英數) 和 `\W` (非英數) 字元之間的界限上。The match must occur on a boundary between a `\w` (alphanumeric) and a `\W` (nonalphanumeric) character. `\b\w+\s\w+\b` "them theme them them" 中的 "them theme"、"them them""them theme", "them them" in "them theme them them"
`\B` 比對不可以發生在 `\b` 界限上。The match must not occur on a `\b` boundary. `\Bend\w*\b` "end sends endure lender" 中的 "ends"、"ender""ends", "ender" in "end sends endure lender"

## 群組建構Grouping Constructs

`(` 子運算式 `)``(` subexpression `)` 擷取符合的子運算式，並指派以一為起始的序號給它。Captures the matched subexpression and assigns it a one-based ordinal number. `(\w)\1` "deep" 中的 "ee""ee" in "deep"
`(?<` name `>` 子運算式 `)``(?<` name `>` subexpression `)` 將符合的子運算式擷取到具名群組中。Captures the matched subexpression into a named group. `(?<double>\w)\k<double>` "deep" 中的 "ee""ee" in "deep"
`(?<` 名稱1 `-` 名稱2 `>` 子運算式 `)``(?<` name1 `-` name2 `>` subexpression `)` 定義對稱群組定義。Defines a balancing group definition. 如需詳細資訊，請參閱 Grouping Constructs中的＜對稱群組定義＞一節。For more information, see the "Balancing Group Definition" section in Grouping Constructs. `(((?'Open'\()[^\(\)]*)+((?'Close-Open'\))[^\(\)]*)+)*(?(Open)(?!))\$` "3+2^((1-3)*(3-1))" 中的 "((1-3)*(3-1))""((1-3)*(3-1))" in "3+2^((1-3)*(3-1))"
`(?:` 子運算式 `)``(?:` subexpression `)` 定義非擷取型群組。Defines a noncapturing group. `Write(?:Line)?` "Console.WriteLine()" 中的 "WriteLine""WriteLine" in "Console.WriteLine()"

"Console.Write(value)" 中的 "Write""Write" in "Console.Write(value)"
`(?imnsx-imnsx:` 子運算式 `)``(?imnsx-imnsx:` subexpression `)` 套用或停用 subexpression內指定的選項。Applies or disables the specified options within subexpression. 如需詳細資訊，請參閱 Regular Expression OptionsFor more information, see Regular Expression Options. `A\d{2}(?i:\w+)\b` "A12xl A12XL a12xl" 中的 "A12xl"、"A12XL""A12xl", "A12XL" in "A12xl A12XL a12xl"
`(?=` 子運算式 `)``(?=` subexpression `)` 零寬度右合樣 (Positive Lookahead) 判斷提示。Zero-width positive lookahead assertion. `\w+(?=\.)` "He is."is", "ran", and "out" in "He is. The dog ran.The dog ran. The sun is out." 中的 "is"、"ran" 和 "out"The sun is out."
`(?!` 子運算式 `)``(?!` subexpression `)` 零寬度右不合樣 (Negative Lookahead) 判斷提示。Zero-width negative lookahead assertion. `\b(?!un)\w+\b` "unsure sure unity used" 中的 "sure"、"used""sure", "used" in "unsure sure unity used"
`(?<=` 子運算式 `)``(?<=` subexpression `)` 零寬度左合樣 (Positive Lookbehind) 判斷提示。Zero-width positive lookbehind assertion. `(?<=19)\d{2}\b` "1851 1999 1950 1905 2003" 中的 "99"、"50"、"05""99", "50", "05" in "1851 1999 1950 1905 2003"
`(?<!` 子運算式 `)``(?<!` subexpression `)` 零寬度左不合樣 (Negative Lookbehind) 判斷提示。Zero-width negative lookbehind assertion. `(?<!19)\d{2}\b` "1851 1999 1950 1905 2003" 中的 "51"、"03""51", "03" in "1851 1999 1950 1905 2003"
`(?>` 子運算式 `)``(?>` subexpression `)` 非回溯子運算式，也稱為窮盡 (Greedy) 子運算式。Nonbacktracking (or "greedy") subexpression. `[13579](?>A+B+)` "1ABB 3ABBC 5AB 5AC" 中的 "1ABB"、"3ABB" 和 "5AB""1ABB", "3ABB", and "5AB" in "1ABB 3ABBC 5AB 5AC"

## 數量詞Quantifiers

`*` 比對上一個項目零次或多次。Matches the previous element zero or more times. `\d*\.\d` ".0"、"19.9"、"219.9"".0", "19.9", "219.9"
`+` 比對上一個項目一次或多次。Matches the previous element one or more times. `"be+"` "been" 中的 "bee"、"bent" 中的 "be""bee" in "been", "be" in "bent"
`?` 比對上一個項目零次或一次。Matches the previous element zero or one time. `"rai?n"` "ran"、"rain""ran", "rain"
`{` n `}``{` n `}` 比對上一個項目剛好 n 次。Matches the previous element exactly n times. `",\d{3}"` "1,043.6" 中的 ",043"、"9,876,543,210" 中的 ",876"、",543" 和 ",210"",043" in "1,043.6", ",876", ",543", and ",210" in "9,876,543,210"
`{` n `,}``{` n `,}` 比對上一個項目至少 n 次。Matches the previous element at least n times. `"\d{2,}"` "166"、"29"、"1930""166", "29", "1930"
`{` n `,` m `}``{` n `,` m `}` 比對上一個項目至少 n 次，但不超過 m 次。Matches the previous element at least n times, but no more than m times. `"\d{3,5}"` "166"、"17668""166", "17668"

"193024" 中的 "19302""19302" in "193024"
`*?` 比對上一個項目零次以上，但越少次越好。Matches the previous element zero or more times, but as few times as possible. `\d*?\.\d` ".0"、"19.9"、"219.9"".0", "19.9", "219.9"
`+?` 比對上一個項目一次或多次，但越少次越好。Matches the previous element one or more times, but as few times as possible. `"be+?"` "been" 中的 "be"、"bent" 中的 "be""be" in "been", "be" in "bent"
`??` 比對上一個項目零次或一次，但越少次越好。Matches the previous element zero or one time, but as few times as possible. `"rai??n"` "ran"、"rain""ran", "rain"
`{` n `}?``{` n `}?` 比對前一個項目剛好 n 次。Matches the preceding element exactly n times. `",\d{3}?"` "1,043.6" 中的 ",043"、"9,876,543,210" 中的 ",876"、",543" 和 ",210"",043" in "1,043.6", ",876", ",543", and ",210" in "9,876,543,210"
`{` n `,}?``{` n `,}?` 比對前一個項目至少 n 次，但愈少次愈好。Matches the previous element at least n times, but as few times as possible. `"\d{2,}?"` "166"、"29"、"1930""166", "29", "1930"
`{` n `,` m `}?``{` n `,` m `}?` 比對前一個項目 n 次到 m 次，但越少次越好。Matches the previous element between n and m times, but as few times as possible. `"\d{3,5}?"` "166"、"17668""166", "17668"

"193024" 中的 "193"、"024""193", "024" in "193024"

## 反向參考建構Backreference Constructs

`\` 數字`\` number 反向參考。Backreference. 比對編號子運算式的值。Matches the value of a numbered subexpression. `(\w)\1` "seek" 中的 "ee""ee" in "seek"
`\k<` name `>``\k<` name `>` 命名的反向參考。Named backreference. 比對具名運算式的值。Matches the value of a named expression. `(?<char>\w)\k<char>` "seek" 中的 "ee""ee" in "seek"

## 交替建構Alternation Constructs

`|` 比對由分隔號 (|) 字元所隔開的任何一個元素。Matches any one element separated by the vertical bar (|) character. `th(e|is|at)` 後面這個字串中的 "the"、"this"："this is the day."the", "this" in "this is the day. ""
`(?(` expression `)` yes `|` no `)``(?(` expression `)` yes `|` no `)` 如果 expression 所指定的規則運算式模式相符，則比對 yes ，否則比對選擇性的 no 部分。Matches yes if the regular expression pattern designated by expression matches; otherwise, matches the optional no part. expression 會解譯為零寬度判斷提示。expression is interpreted as a zero-width assertion. `(?(A)A\d{2}\b|\b\d{3}\b)` "A10 C103 910" 中的 "A10"、"910""A10", "910" in "A10 C103 910"
`(?(` name `)` yes `|` no `)``(?(` name `)` yes `|` no `)` 如果 name (具名或編號擷取群組) 有相符項目，則比對 yes，否則比對選擇性的 noMatches yes if name, a named or numbered capturing group, has a match; otherwise, matches the optional no. `(?<quoted>")?(?(quoted).+?"|\S+\s)` "Dogs.jpg "Yiska playing.jpg"" 中的 Dogs.jpg、"Yiska playing.jpg"Dogs.jpg, "Yiska playing.jpg" in "Dogs.jpg "Yiska playing.jpg""

## 替代Substitutions

`\$` number`\$` number 替代群組 number所比對的子字串。Substitutes the substring matched by group number. `\b(\w+)(\s)(\w+)\b` `\$3\$2\$1` "one two""one two" "two one""two one"
`\${` name `}``\${` name `}` 替代具名群組 name所比對的子字串。Substitutes the substring matched by the named group name. `\b(?<word1>\w+)(\s)(?<word2>\w+)\b` `\${word2} \${word1}` "one two""one two" "two one""two one"
`\$\$` 替代常值 "\$"。Substitutes a literal "\$". `\b(\d+)\s?USD` `\$\$\$1` "103 USD""103 USD" "\$103""\$103"
`\$&` 替代整個符合項目的複本。Substitutes a copy of the whole match. `\\$?\d*\.?\d+` `**\$&**` "\$1.30""\$1.30" "**\$1.30**""**\$1.30**"
`\$`` 替代輸入字串中符合字串前面的所有文字。Substitutes all the text of the input string before the match. `B+` `\$`` "AABBCC""AABBCC" "AAAACC""AAAACC"
`\$'` 替代輸入字串中符合字串後面的所有文字。Substitutes all the text of the input string after the match. `B+` `\$'` "AABBCC""AABBCC" "AACCCC""AACCCC"
`\$+` 替代已擷取的最後一個群組。Substitutes the last group that was captured. `B+(C+)` `\$+` "AABBCCDD""AABBCCDD" AACCDDAACCDD
`\$_` 替代整個輸入字串。Substitutes the entire input string. `B+` `\$_` "AABBCC""AABBCC" "AAAABBCCCC""AAAABBCCCC"

## 規則運算式選項Regular Expression Options

• 使用 其他建構 `(?imnsx-imnsx)`，其中選項或選項集合前面的減號 (-) 會關閉這些選項。By using the miscellaneous construct `(?imnsx-imnsx)`, where a minus sign (-) before an option or set of options turns those options off. 例如， `(?i-mn)` 會開啟不區分大小寫的比對 (`i`)、關閉多行模式 (`m`)，以及關閉未命名的群組擷取 (`n`)。For example, `(?i-mn)` turns case-insensitive matching (`i`) on, turns multiline mode (`m`) off, and turns unnamed group captures (`n`) off. 選項會從定義該選項的位置開始套用至規則運算式模式並且保持生效，直到模式結尾或出現另一個建構反轉選項為止。The option applies to the regular expression pattern from the point at which the option is defined, and is effective either to the end of the pattern or to the point where another construct reverses the option.

• 使用 grouping construct`(?imnsx-imnsx:`子運算式`)`，只針對指定的群組定義選項。By using the grouping construct`(?imnsx-imnsx:`subexpression`)`, which defines options for the specified group only.

.NET 規則運算式引擎支援下列內嵌選項。The .NET regular expression engine supports the following inline options.

`i` 使用不區分大小寫的比對方式。Use case-insensitive matching. `\b(?i)a(?-i)a\w+\b` "aardvark AAAuto aaaAuto Adam breakfast" 中的 "aardvark"、"aaaAuto""aardvark", "aaaAuto" in "aardvark AAAuto aaaAuto Adam breakfast"
`m` 使用多行模式。Use multiline mode. `^``\$` 符合行的開頭與結尾，而不是字串的開始和結尾。`^` and `\$` match the beginning and end of a line, instead of the beginning and end of a string. 如需範例，請參閱 Regular Expression Options中的＜多行模式＞一節。For an example, see the "Multiline Mode" section in Regular Expression Options.
`n` 不擷取未命名的群組。Do not capture unnamed groups. 如需範例，請參閱 Regular Expression Options中的＜僅明確擷取＞一節。For an example, see the "Explicit Captures Only" section in Regular Expression Options.
`s` 使用單行模式。Use single-line mode. 如需範例，請參閱 Regular Expression Options中的＜單行模式＞一節。For an example, see the "Single-line Mode" section in Regular Expression Options.
`x` 忽略規則運算式模式中未逸出的空白字元。Ignore unescaped white space in the regular expression pattern. `\b(?x) \d+ \s \w+` "1 aardvark 2 cats IV centurions" 中的 "1 aardvark"、"2 cats""1 aardvark", "2 cats" in "1 aardvark 2 cats IV centurions"

## 其他建構Miscellaneous Constructs

`(?imnsx-imnsx)` 在模式中間設定或停用選項，例如不區分大小寫。如需詳細資訊，請參閱規則運算式選項Sets or disables options such as case insensitivity in the middle of a pattern.For more information, see Regular Expression Options. `\bA(?i)b\w+\b` 會比對 "ABA Able Act" 中的 "ABA"、"Able"`\bA(?i)b\w+\b` matches "ABA", "Able" in "ABA Able Act"
`(?#` 註解 `)``(?#` comment `)` 內嵌註解。Inline comment. 註解會在第一個右括號結束。The comment ends at the first closing parenthesis. `\bA(?#Matches words starting with A)\w+\b`
`#` [至行尾]`#` [to end of line] X 模式註解。X-mode comment. 註解從未逸出的 `#` 開始，並延續到行尾。The comment starts at an unescaped `#` and continues to the end of the line. `(?x)\bA\w+\b#Matches words starting with A`