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

規則運算式是規則運算式引擎嘗試在輸入文字中比對的模式。A regular expression is a pattern that the regular expression engine attempts to match in input text. 模式是由一個或多個字元常值、運算子或建構所組成。A pattern consists of one or more character literals, operators, or constructs. 如需簡介,請參閱 .NET 規則運算式For a brief introduction, see .NET Regular Expressions.

本快速參考的每一節列出您可以用於定義規則運算式的特定某類字元、運算子和建構:Each section in this quick reference lists a particular category of characters, operators, and constructs that you can use to define regular expressions:

逸出字元Character escapes
字元類別Character classes
錨點Anchors
群組建構Grouping constructs
數量詞Quantifiers
反向參考建構Backreference constructs
替代建構Alternation constructs
替代Substitutions
規則運算式選項Regular expression options
其他建構Miscellaneous constructs

我們也有用兩種格式提供此資訊,可讓您下載及列印,以方便參考。We’ve also provided this information in two formats that you can download and print for easy reference:

下載 Word (.docx) 格式Download in Word (.docx) format
下載 PDF (.pdf) 格式Download in PDF (.pdf) format

逸出字元Character Escapes

規則運算式中的反斜線字元 (\) 表示接在後面的字元是特殊字元 (如下表所示),或應該解譯為常值。The backslash character (\) in a regular expression indicates that the character that follows it either is a special character (as shown in the following table), or should be interpreted literally. 如需詳細資訊,請參閱逸出字元For more information, see Character Escapes.

逸出的字元Escaped character 描述Description 模式Pattern 符合Matches
\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
比對 Xx所指定的 ASCII 控制字元,其中 Xx 是控制字元的字母。Matches the ASCII control character that is specified by X or x, where X or x is the letter of the control character. \cC "\x0003" (Ctrl-C) 中的 "\x0003""\x0003" in "\x0003" (Ctrl-C)
\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"

回到頁首Back to top

字元類別Character Classes

字元類別會比對一組字元中的任何一個字元。A character class matches any one of a set of characters. 字元類別包含下表列出的語言項目。Character classes include the language elements listed in the following table. 如需詳細資訊,請參閱 Character ClassesFor more information, see Character Classes.

字元類別Character class 描述Description 模式Pattern 符合Matches
[ 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.

若要比對常值句號字元 (.To match a literal period character (. \u002E),您必須在前面加上逸出字元 (\.)。or \u002E), you must precede it with the escape character (\.).
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"

回到頁首Back to top

錨點Anchors

錨點 (即原子零寬度判斷提示) 會造成根據字串中的目前位置來決定比對成功或失敗,但不會造成引擎在字串中前進或是取用字元。Anchors, or atomic zero-width assertions, cause a match to succeed or fail depending on the current position in the string, but they do not cause the engine to advance through the string or consume characters. 下表列出的 metacharacter 是錨點。The metacharacters listed in the following table are anchors. 如需詳細資訊,請參閱 錨點For more information, see Anchors.

判斷提示Assertion 描述Description 模式Pattern 符合Matches
^ 根據預設,比對必須在字串的開頭開始;在多行模式中,它必須在一行的開頭開始。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"

回到頁首Back to top

群組建構Grouping Constructs

群組建構會描寫規則運算式的子運算式,而且通常會擷取輸入字串的子字串。Grouping constructs delineate subexpressions of a regular expression and typically capture substrings of an input string. 群組建構包含下表列出的語言項目。Grouping constructs include the language elements listed in the following table. 如需詳細資訊,請參閱 Grouping ConstructsFor more information, see Grouping Constructs.

群組建構Grouping construct 描述Description 模式Pattern 符合Matches
( 子運算式 )( 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"

回到頁首Back to top

數量詞Quantifiers

數量詞指定上一個項目 (可能是字元、群組或字元類別) 必須在輸入字串中出現多少次,才算符合。A quantifier specifies how many instances of the previous element (which can be a character, a group, or a character class) must be present in the input string for a match to occur. 數量詞包含下表列出的語言項目。Quantifiers include the language elements listed in the following table. 如需詳細資訊,請參閱 QuantifiersFor more information, see Quantifiers.

數量詞Quantifier 描述Description 模式Pattern 符合Matches
* 比對上一個項目零次或多次。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"

回到頁首Back to top

反向參考建構Backreference Constructs

反向參考可於後來在相同規則運算式中再識別先前符合的子運算式。A backreference allows a previously matched subexpression to be identified subsequently in the same regular expression. 下表列出 .NET 中規則運算式所支援的反向參考建構。The following table lists the backreference constructs supported by regular expressions in .NET. 如需詳細資訊,請參閱 Backreference ConstructsFor more information, see Backreference Constructs.

反向參考建構Backreference construct 描述Description 模式Pattern 符合Matches
\ 數字\ 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"

回到頁首Back to top

交替建構Alternation Constructs

交替建構會修改規則運算式來啟用二選一比對。Alternation constructs modify a regular expression to enable either/or matching. 這些建構包含下表列出的語言項目。These constructs include the language elements listed in the following table. 如需詳細資訊,請參閱 Alternation ConstructsFor more information, see Alternation Constructs.

交替建構Alternation construct 描述Description 模式Pattern 符合Matches
| 比對由分隔號 (|) 字元所隔開的任何一個元素。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""

回到頁首Back to top

替代Substitutions

替代是取代模式中支援的規則運算式語言項目。Substitutions are regular expression language elements that are supported in replacement patterns. 如需詳細資訊,請參閱 SubstitutionsFor more information, see Substitutions. 下表列出的 metacharacter 是原子零寬度判斷提示。The metacharacters listed in the following table are atomic zero-width assertions.

字元Character 描述Description 模式Pattern 取代模式Replacement pattern 輸入字串Input string 結果字串Result string
$ 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"

回到頁首Back to top

規則運算式選項Regular Expression Options

您可以指定選項,以控制規則運算式引擎如何解譯規則運算式模式。You can specify options that control how the regular expression engine interprets a regular expression pattern. 這些選項中有許多可以指定為內嵌 (在規則運算式模式中) 或是一個或多個 RegexOptions 常數。Many of these options can be specified either inline (in the regular expression pattern) or as one or more RegexOptions constants. 這個快速參考只列出內嵌選項。This quick reference lists only inline options. 如需內嵌和 RegexOptions 選項的詳細資訊,請參閱 Regular Expression OptionsFor more information about inline and RegexOptions options, see the article Regular Expression Options.

您可以透過兩種方式指定內嵌選項:You can specify an inline option in two ways:

  • 使用 其他建構 (?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.

選項Option 描述Description 模式Pattern 符合Matches
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"

回到頁首Back to top

其他建構Miscellaneous Constructs

其他建構會修改規則運算式模式或提供其相關資訊。Miscellaneous constructs either modify a regular expression pattern or provide information about it. 下表列出 .NET 所支援的其他建構。The following table lists the miscellaneous constructs supported by .NET. 如需詳細資訊,請參閱 Miscellaneous ConstructsFor more information, see Miscellaneous Constructs.

建構Construct 定義Definition 範例Example
(?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

請參閱See Also

System.Text.RegularExpressions
Regex
規則運算式Regular Expressions
規則運算式類別Regular Expression Classes
規則運算式範例Regular Expression Examples
規則運算式 - 快速參考 (以 Word 格式下載)Regular Expressions - Quick Reference (download in Word format)
規則運算式 - 快速參考 (以 PDF 格式下載)Regular Expressions - Quick Reference (download in PDF format)