正則表達(dá)式是正則表達(dá)式引擎嘗試匹配輸入文本的一種模式哈肖。 模式由一個(gè)或多個(gè)字符文本练俐、運(yùn)算符或構(gòu)造組成注暗。 有關(guān)簡要介紹,請參閱 .NET 正則表達(dá)式猫牡。
此快速參考中的每一節(jié)都列出了可用于定義正則表達(dá)式的字符胡诗、運(yùn)算符和構(gòu)造的一種特定類別。
字符轉(zhuǎn)義 Character Escapes
正則表達(dá)式中的反斜杠字符 () 指示其后跟的字符是特殊字符(如下表所示),或應(yīng)按原義解釋該字符煌恢。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. 有關(guān)詳細(xì)信息骇陈,請參閱字符轉(zhuǎn)義。For more information, see Character Escapes.
轉(zhuǎn)義字符 Escaped character | 描述 Description | 模式 Pattern | 匹配 Matches |
---|---|---|---|
\a |
與報(bào)警 (bell) 符 \u0007 匹配瑰抵。Matches a bell character, \u0007. | \a |
"Error!" + '\u0007' 中的 "\u0007"``"\u0007" in "Error!" + '\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 |
與回車符 \u000D 匹配婿崭。Matches 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 |
與轉(zhuǎn)義符 \u001B 匹配婿着。Matches an escape, \u001B. | \e |
"\x001B" 中的 "\x001B"``"\x001B" in "\x001B"
|
\ nnn\ nnn
|
使用八進(jìn)制表示形式指定字符(nnn 由二位或三位數(shù)字組成)授瘦。Uses octal representation to specify a character (nnn consists of two or three digits). | \w\040\w |
"a bc d" 中的 "a b" 和 "c d"``"a b" , "c d" in "a bc d"
|
\x nn\x nn
|
使用十六進(jìn)制表示形式指定字符(nn 恰好由兩位數(shù)字組成)。Uses hexadecimal representation to specify a character (nn consists of exactly two digits). | \w\x20\w |
"a bc d" 中的 "a b" 和 "c d"``"a b" , "c d" in "a bc d"
|
\c X\c X \c x \c x
|
匹配 X 或 x 指定的 ASCII 控制字符竟宋,其中 X 或 x 是控制字符的字母提完。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" 中的 "\x0003" (Ctrl-C)"\x0003" in "\x0003" (Ctrl-C) |
\u nnnn\u nnnn
|
使用十六進(jìn)制表示形式匹配 Unicode 字符(由 nnnn 正確表示的四位數(shù))。Matches a Unicode character by using hexadecimal representation (exactly four digits, as represented by nnnn). | \w\u0020\w |
"a bc d" 中的 "a b" 和 "c d"``"a b" , "c d" in "a bc d"
|
\ |
在后面帶有不識(shí)別為本主題的此表和其他表中的轉(zhuǎn)義符的字符時(shí)丘侠,與該字符匹配氯葬。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 . 這樣一來官研,正則表達(dá)式引擎可以區(qū)分語言元素(如 * 或 ?)和字符文本(由 \* 或 \? 表示)秽澳。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
字符類與一組字符中的任何一個(gè)字符匹配。A character class matches any one of a set of characters. 字符類包括下表中列出的語言元素戏羽。Character classes include the language elements listed in the following table. 有關(guān)更多信息担神,請參見 字符類。For more information, see Character Classes.
字符類 Character class | 描述 Description | 模式 Pattern | 匹配 Matches |
---|---|---|---|
[ character_group ] [ character_group ]
|
匹配 character_group 中的任何單個(gè)字符始花。Matches any single character in character_group. 默認(rèn)情況下妄讯,匹配區(qū)分大小寫酷宵。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 中的任意單個(gè)字符匹配 浇垦。Negation: Matches any single character that is not in character_group. 默認(rèn)情況下炕置, character_group 中的字符區(qū)分大小寫。By default, characters in character_group are case-sensitive. | [^aei] |
"reign" 中的 "r" 默垄、"g" 和 "n"``"r" , "g" , "n" in "reign"
|
[ first - last ] [ first - last ]
|
字符范圍:與從 first 到 last 的范圍中的任意單個(gè)字符匹配甚纲。Character range: Matches any single character in the range from first to last. | [A-Z] |
"AB123" 中的 "A" 和 "B"``"A" , "B" in "AB123"
|
. |
通配符:與除 \n 之外的任意單個(gè)字符匹配介杆。Wildcard: Matches any single character except \n. 若要匹配文本句點(diǎn)字符(.To match a literal period character (. 或 \u002E )这溅,你必須在該字符前面加上轉(zhuǎn)義符 (\. )。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 }
|
與 name 指定的 Unicode 通用類別或命名塊中的任何單個(gè)字符匹配癞尚。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 }
|
與不在 name 指定的 Unicode 通用類別或命名塊中的任何單個(gè)字符匹配仪壮。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 |
與任何十進(jìn)制數(shù)字匹配概行。Matches any decimal digit. | \d |
"4 = IV" 中的 "4"``"4" in "4 = IV"
|
\D |
匹配不是十進(jìn)制數(shù)的任意字符业踏。Matches any character other than a decimal digit. | \D |
"4 = IV" 中的 " " 勤家、"=" 伐脖、" " 讼庇、"I" 和 "V"``" " , "=" , " " , "I" , "V" in "4 = IV"
|
定位點(diǎn) Anchors
定位點(diǎn)或原子零寬度斷言會(huì)使匹配成功或失敗蠕啄,具體取決于字符串中的當(dāng)前位置歼跟,但它們不會(huì)使引擎在字符串中前進(jìn)或使用字符。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. 下表中列出的元字符是定位點(diǎn)。The metacharacters listed in the following table are anchors. 有關(guān)詳細(xì)信息坪它,請參閱 定位點(diǎn)往毡。For more information, see Anchors.
斷言 Assertion | 說明 Description | 模式 Pattern | 匹配 Matches |
---|---|---|---|
^ |
默認(rèn)情況下懒震,必須從字符串的開頭開始匹配;在多行模式中递宅,必須從該行的開頭開始办龄。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"``"901" in "901-333-"
|
$ |
默認(rèn)情況下,匹配必須出現(xiàn)在字符串的末尾英融,或在字符串末尾的 \n 之前矢赁;在多行模式中撩银,必須出現(xià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}$ |
"-901-333" 中的 "-333"``"-333" in "-901-333"
|
\A |
匹配必須出現(xiàn)在字符串的開頭。The match must occur at the start of the string. | \A\d{3} |
"901-333-" 中的 "901"``"901" in "901-333-"
|
\Z |
匹配必須出現(xiàn)在字符串的末尾或出現(xiàn)在字符串末尾的 \n 之前奥喻。The match must occur at the end of the string or before \n at the end of the string. |
-\d{3}\Z |
"-901-333" 中的 "-333"``"-333" in "-901-333"
|
\z |
匹配必須出現(xiàn)在字符串的末尾。The match must occur at the end of the string. | -\d{3}\z |
"-901-333" 中的 "-333"``"-333" in "-901-333"
|
\G |
匹配必須出現(xiàn)在上一個(gè)匹配結(jié)束的地方冷离。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 |
匹配必須出現(xiàn)在 \w (字母數(shù)字)和 \W (非字母數(shù)字)字符之間的邊界上结耀。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 |
匹配不得出現(xiàn)在 \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"
|
分組構(gòu)造 Grouping Constructs
分組構(gòu)造描述了正則表達(dá)式的子表達(dá)式黑毅,通常用于捕獲輸入字符串的子字符串。Grouping constructs delineate subexpressions of a regular expression and typically capture substrings of an input string. 分組構(gòu)造包括下表中列出的語言元素缚去。Grouping constructs include the language elements listed in the following table. 有關(guān)詳細(xì)信息易结,請參閱 分組構(gòu)造搞动。For more information, see Grouping Constructs.
分組構(gòu)造 Grouping construct | 描述 Description | 模式 Pattern | 匹配 Matches |
---|---|---|---|
( subexpression ) ( subexpression )
|
捕獲匹配的子表達(dá)式并將其分配到一個(gè)從 1 開始的序號(hào)中。Captures the matched subexpression and assigns it a one-based ordinal number. | (\w)\1 |
"deep" 中的 "ee"``"ee" in "deep"
|
(?< name > subexpression ) (?< name > subexpression )
|
將匹配的子表達(dá)式捕獲到一個(gè)命名組中箩溃。Captures the matched subexpression into a named group. | (?\w)\k |
"deep" 中的 "ee"``"ee" in "deep"
|
(?< name1 - name2 > subexpression ) (?< name1 - name2 > subexpression )
|
定義平衡組定義。Defines a balancing group definition. 有關(guān)詳細(xì)信息,請參閱 分組構(gòu)造中的 "平衡組定義" 部分魁瞪。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 ) (?: 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: subexpression ) (?imnsx-imnsx: subexpression )
|
應(yīng)用或禁用 子表達(dá)式中指定的選項(xiàng)辅髓。Applies or disables the specified options within subexpression. 有關(guān)詳細(xì)信息洛口,請參閱 正則表達(dá)式選項(xiàng)第焰。For more information, see Regular Expression Options. | A\d{2}(?i:\w+)\b |
"A12xl A12XL a12xl" 中的 "A12xl" 和 "A12XL"``"A12xl" , "A12XL" in "A12xl A12XL a12xl"
|
(?= subexpression ) (?= subexpression )
|
零寬度正預(yù)測先行斷言烘跺。Zero-width positive lookahead assertion. | \w+(?=\.) |
"He is. The dog ran. The sun is out." 中的 "is" 梧喷、"ran" 和 "out"``"is" , "ran" , and "out" in "He is. The dog ran. The sun is out."
|
(?! subexpression ) (?! subexpression )
|
零寬度負(fù)預(yù)測先行斷言伤柄。Zero-width negative lookahead assertion. | \b(?!un)\w+\b |
"unsure sure unity used" 中的 "sure" 和 "used"``"sure" , "used" in "unsure sure unity used"
|
(?<= subexpression ) (?<= subexpression )
|
零寬度正回顧后發(fā)斷言。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 ) (? subexpression )
|
零寬度負(fù)回顧后發(fā)斷言。Zero-width negative lookbehind assertion. | `(? |
"1851 1999 1950 1905 2003" 中的 "51" 和 "03"``"51" , "03" in "1851 1999 1950 1905 2003"
|
(?> subexpression ) (?> subexpression )
|
原子組奄毡。Atomic group. | [13579](?>A+B+) |
"1ABB 3ABBC 5AB 5AC" 中的 "1ABB" 吼过、"3ABB" 和 "5AB"``"1ABB" , "3ABB" , and "5AB" in "1ABB 3ABBC 5AB 5AC"
|
數(shù)量詞 Quantifiers
限定符指定在輸入字符串中必須存在上一個(gè)元素(可以是字符酱床、組或字符類)的多少個(gè)實(shí)例才能出現(xiàn)匹配項(xiàng)扇谣。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. 有關(guān)更多信息衩茸,請參見 數(shù)量詞。For more information, see Quantifiers.
限定符 Quantifier | 描述 Description | 模式 Pattern | 匹配 Matches |
---|---|---|---|
* |
匹配上一個(gè)元素零次或多次囊蓝。Matches the previous element zero or more times. | \d*\.\d |
".0" , "19.9" , "219.9"``".0" , "19.9" , "219.9"
|
+ |
匹配上一個(gè)元素一次或多次聚霜。Matches the previous element one or more times. | "be+" |
"been" 中的 "bee" 、"bent" 中的 "be"``"bee" in "been" , "be" in "bent"
|
? |
匹配上一個(gè)元素零次或一次姥芥。Matches the previous element zero or one time. | "rai?n" |
"ran" 凉唐,"rain"``"ran" , "rain"
|
{ n } { n }
|
匹配上一個(gè)元素恰好 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 ,}
|
匹配上一個(gè)元素至少 n 次。Matches the previous element at least n times. | "\d{2,}" |
"166" , "29" , "1930"``"166" , "29" , "1930"
|
{ n , m } { n , m }
|
匹配上一個(gè)元素至少 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"
|
*? |
匹配上一個(gè)元素零次或多次锄俄,但次數(shù)盡可能少奶赠。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"
|
+? |
匹配上一個(gè)元素一次或多次,但次數(shù)盡可能少苇经。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"
|
?? |
匹配上一個(gè)元素零次或一次,但次數(shù)盡可能少兼都。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 ,}?
|
匹配上一個(gè)元素至少 n 次,但次數(shù)盡可能少描函。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 }?
|
匹配上一個(gè)元素的次數(shù)介于 n 和 m 之間,但次數(shù)盡可能少基公。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"
|
反向引用構(gòu)造 Backreference Constructs
反向引用允許在同一正則表達(dá)式中隨后標(biāo)識(shí)以前匹配的子表達(dá)式。A backreference allows a previously matched subexpression to be identified subsequently in the same regular expression. 下表列出了 .NET 正則表達(dá)式支持的反向引用構(gòu)造斑司。The following table lists the backreference constructs supported by regular expressions in .NET. 有關(guān)詳細(xì)信息宿刮,請參閱 反向引用構(gòu)造。For more information, see Backreference Constructs.
反向引用構(gòu)造 Backreference construct | 描述 Description | 模式 Pattern | 匹配 Matches |
---|---|---|---|
\ number\ number
|
后向引用磕潮。Backreference. 匹配編號(hào)子表達(dá)式的值之景。Matches the value of a numbered subexpression. | (\w)\1 |
"seek" 中的 "ee"``"ee" in "seek"
|
\k< name >``\k< name >
|
命名后向引用锻狗。Named backreference. 匹配命名表達(dá)式的值。Matches the value of a named expression. | (?\w)\k |
"seek" 中的 "ee"``"ee" in "seek"
|
替換構(gòu)造 Alternation Constructs
替換構(gòu)造用于修改正則表達(dá)式以啟用 either/or 匹配。Alternation constructs modify a regular expression to enable either/or matching. 這些構(gòu)造包括下表中列出的語言元素讲岁。These constructs include the language elements listed in the following table. 有關(guān)詳細(xì)信息,請參閱 替換構(gòu)造阶淘。For more information, see Alternation Constructs.
替換構(gòu)造 Alternation construct | 描述 Description | 模式 Pattern | 匹配 Matches | |||||
---|---|---|---|---|---|---|---|---|
` | ` | 匹配以豎線 (` | ) 字符分隔的任何一個(gè)元素。Matches any one element separated by the vertical bar ( |
`) character. | `th(e | is | at)` |
"this is the day." 中的 "the" 和 "this"``"the" , "this" in "this is the day."
|
(?( expression ) yes ` |
*no* )``(?(*expression* )*yes*
|
*no* )` |
如果由 expression 指定的正則表達(dá)式模式匹配澈蚌,則匹配 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 (已命名或已編號(hào)的捕獲組)具有匹項(xiàng)均践,則匹配 yes鞭铆;否則车遂,匹配可的 no。Matches yes if name, a named or numbered capturing group, has a match; otherwise, matches the optional no. | `(?")?(?(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
替換是替換模式中支持的正則表達(dá)式語言元素彬呻。Substitutions are regular expression language elements that are supported in replacement patterns. 有關(guān)更多信息衣陶,請參見 替代。For more information, see Substitutions. 下表中列出的元字符是原子零寬度斷言闸氮。The metacharacters listed in the following table are atomic zero-width assertions.
字符 Character | 描述 Description | 模式 Pattern | 替換模式 Replacement pattern | 輸入字符串 Input string | 結(jié)果字符串 Result string |
---|---|---|---|---|---|
$ number$ number
|
替換按組 number 匹配的子字符串剪况。Substitutes the substring matched by group number. | \b(\w+)(\s)(\w+)\b |
$3$2$1 |
"one two" |
"two one" |
${ name }``${ name }
|
替換按命名組 name 匹配的子字符串蒲跨。Substitutes the substring matched by the named group name. | \b(?\w+)(\s)(?\w+)\b |
${word2} ${word1} |
"one two" |
"two one" |
$$ |
替換字符 " |
\b(\d+)\s?USD |
$$$1 |
"103 USD" |
"$103" |
$& |
替換整個(gè)匹配項(xiàng)的一個(gè)副本。Substitutes a copy of the whole match. | \$?\d*\.?\d+ |
**$&** |
"$1.30" |
"**$1.30**" |
`$`` | 替換匹配前的輸入字符串的所有文本或悲。Substitutes all the text of the input string before the match. | B+ |
`$`` | "AABBCC" |
"AAAACC" |
$' |
替換匹配后的輸入字符串的所有文本孙咪。Substitutes all the text of the input string after the match. | B+ |
$' |
"AABBCC" |
"AACCCC" |
$+ |
替換最后捕獲的組。Substitutes the last group that was captured. | B+(C+) |
$+ |
"AABBCCDD" |
"AACCDD" |
$_ |
替換整個(gè)輸入字符串巡语。Substitutes the entire input string. | B+ |
$_ |
"AABBCC" |
"AAAABBCCCC" |
正則表達(dá)式選項(xiàng) Regular Expression Options
可以指定控制正則表達(dá)式引擎如何解釋正則表達(dá)式模式的選項(xiàng)该贾。You can specify options that control how the regular expression engine interprets a regular expression pattern. 其中的許多選項(xiàng)可以指定為內(nèi)聯(lián)(在正則表達(dá)式模式中)或指定為一個(gè)或多個(gè) RegexOptions 常量。Many of these options can be specified either inline (in the regular expression pattern) or as one or more RegexOptions constants. 本快速參考僅列出內(nèi)聯(lián)選項(xiàng)捌臊。This quick reference lists only inline options. 有關(guān)內(nèi)聯(lián)和 RegexOptions 選項(xiàng)的詳細(xì)信息杨蛋,請參閱文章 正則表達(dá)式選項(xiàng)。For more information about inline and RegexOptions options, see the article Regular Expression Options.
可通過兩種方式指定內(nèi)聯(lián)選項(xiàng):You can specify an inline option in two ways:
- 通過使用其他構(gòu)造
(?imnsx-imnsx)
理澎,可用選項(xiàng)或選項(xiàng)組前的減號(hào) (-) 關(guān)閉這些選項(xiàng)逞力。By using the miscellaneous construct(?imnsx-imnsx)
, where a minus sign (-) before an option or set of options turns those options off. 例如,(?i-mn)
啟用不區(qū)分大小寫的匹配 (i
)糠爬,關(guān)閉多行模式 (m
) 并關(guān)閉未命名的組捕獲 (n
)寇荧。For example,(?i-mn)
turns case-insensitive matching (i
) on, turns multiline mode (m
) off, and turns unnamed group captures (n
) off. 該選項(xiàng)自定義選項(xiàng)的點(diǎn)開始應(yīng)用于此正則表達(dá)式,且持續(xù)有效直到模式結(jié)束或者到另一構(gòu)造反轉(zhuǎn)此選項(xiàng)的點(diǎn)执隧。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. - 通過使用 分組構(gòu)造
(?imnsx-imnsx:
子表達(dá)式)
(只定義指定組的選項(xiàng))揩抡。By using the grouping construct(?imnsx-imnsx:
subexpression)
, which defines options for the specified group only.
.NET 正則表達(dá)式引擎支持以下內(nèi)聯(lián)選項(xiàng):The .NET regular expression engine supports the following inline options:
選項(xiàng) Option | 描述 Description | 模式 Pattern | 匹配 Matches |
---|---|---|---|
i |
使用不區(qū)分大小寫的匹配户侥。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. ^ 和 $ 匹配行的開頭和結(jié)尾峦嗤,但不匹配字符串的開頭和結(jié)尾胎挎。^ and $ match the beginning and end of a line, instead of the beginning and end of a string. |
有關(guān)示例吧秕,請參閱 正則表達(dá)式選項(xiàng)中的 "多行模式" 部分硕勿。For an example, see the "Multiline Mode" section in Regular Expression Options. | |
n |
不捕獲未命名的組撬腾。Do not capture unnamed groups. | 有關(guān)示例,請參閱 正則表達(dá)式選項(xiàng)中的 "僅顯式捕獲" 部分装黑。For an example, see the "Explicit Captures Only" section in Regular Expression Options. | |
s |
使用單行模式副瀑。Use single-line mode. | 有關(guān)示例,請參閱 正則表達(dá)式選項(xiàng)中的 "單行模式" 部分恋谭。For an example, see the "Single-line Mode" section in Regular Expression Options. | |
x |
忽略正則表達(dá)式模式中的非轉(zhuǎn)義空白糠睡。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"
|
其他構(gòu)造 Miscellaneous Constructs
其他構(gòu)造可修改某個(gè)正則表達(dá)式模式或提供有關(guān)該模式的信息。Miscellaneous constructs either modify a regular expression pattern or provide information about it. 下表列出了 .NET 支持的其他構(gòu)造疚颊。The following table lists the miscellaneous constructs supported by .NET. 有關(guān)詳細(xì)信息狈孔,請參閱 其他構(gòu)造。For more information, see Miscellaneous Constructs.
構(gòu)造 Construct | 定義 Definition | 示例 Example |
---|---|---|
(?imnsx-imnsx) |
在模式中間對(duì)諸如不區(qū)分大小寫這樣的選項(xiàng)進(jìn)行設(shè)置或禁用串稀。有關(guān)詳細(xì)信息,請參閱正則表達(dá)式選項(xiàng)狮杨。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 ) (?# comment )
|
內(nèi)聯(lián)注釋母截。Inline comment. 該注釋在第一個(gè)右括號(hào)處終止。The comment ends at the first closing parenthesis. | \bA(?#Matches words starting with A)\w+\b |
# [至行尾]# [to end of line] |
X 模式注釋橄教。X-mode comment. 該注釋以非轉(zhuǎn)義的 # 開頭清寇,并繼續(xù)到行的結(jié)尾。The comment starts at an unescaped # and continues to the end of the line. |
(?x)\bA\w+\b#Matches words starting with A |