正则表达式是正则表达式引擎尝试匹配输入文本的一种模式。 模式由一个或多个字符文本、运算符或构造组成。 有关简要介绍,请参阅 .NET 正则表达式。
此快速参考中的每一节都列出了可用于定义正则表达式的字符、运算符和构造的一种特定类别。
字符转义 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 |
与报警 (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 |
与转义符 \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 bc d" 中的 "a b" 和 "c d"``"a b" , "c d" in "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 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 |
使用十六进制表示形式匹配 Unicode 字符(由 nnnn 正确表示的四位数)。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" |
\ |
在后面带有不识别为本主题的此表和其他表中的转义符的字符时,与该字符匹配。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
字符类与一组字符中的任何一个字符匹配。A character class matches any one of a set of characters. 字符类包括下表中列出的语言元素。Character classes include the language elements listed in the following table. 有关更多信息,请参见 字符类。For 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" |
[ first - last ] [ first - last ] |
字符范围:与从 first 到 last 的范围中的任意单个字符匹配。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 } |
与 name 指定的 Unicode 通用类别或命名块中的任何单个字符匹配。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 通用类别或命名块中的任何单个字符匹配。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
定位点或原子零宽度断言会使匹配成功或失败,具体取决于字符串中的当前位置,但它们不会使引擎在字符串中前进或使用字符。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. 下表中列出的元字符是定位点。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"``"901" in "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}$ |
"-901-333" 中的 "-333"``"-333" in "-901-333" |
\A |
匹配必须出现在字符串的开头。The match must occur at the start of the string. | \A\d{3} |
"901-333-" 中的 "901"``"901" in "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 |
"-901-333" 中的 "-333"``"-333" in "-901-333" |
\z |
匹配必须出现在字符串的末尾。The match must occur at the end of the string. | -\d{3}\z |
"-901-333" 中的 "-333"``"-333" in "-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
分组构造描述了正则表达式的子表达式,通常用于捕获输入字符串的子字符串。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. 有关详细信息,请参阅 分组构造。For more information, see Grouping Constructs.
分组构造 Grouping construct | 描述 Description | 模式 Pattern | 匹配 Matches |
---|---|---|---|
( subexpression ) ( subexpression ) |
捕获匹配的子表达式并将其分配到一个从 1 开始的序号中。Captures the matched subexpression and assigns it a one-based ordinal number. | (\w)\1 |
"deep" 中的 "ee"``"ee" in "deep" |
(?< name > subexpression ) (?< name > subexpression ) |
将匹配的子表达式捕获到一个命名组中。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. 有关详细信息,请参阅 分组构造中的 "平衡组定义" 部分。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 ) |
应用或禁用 子表达式中指定的选项。Applies or disables the specified options within subexpression. 有关详细信息,请参阅 正则表达式选项。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 ) |
零宽度正预测先行断言。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 ) |
零宽度负预测先行断言。Zero-width negative lookahead assertion. | \b(?!un)\w+\b |
"unsure sure unity used" 中的 "sure" 和 "used"``"sure" , "used" in "unsure sure unity used" |
(?<= subexpression ) (?<= subexpression ) |
零宽度正回顾后发断言。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 ) |
零宽度负回顾后发断言。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" |
数量词 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. 有关更多信息,请参见 数量词。For 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" |
反向引用构造 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. 有关详细信息,请参阅 反向引用构造。For more information, see Backreference Constructs.
反向引用构造 Backreference construct | 描述 Description | 模式 Pattern | 匹配 Matches |
---|---|---|---|
\ number\ 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. | (?\w)\k |
"seek" 中的 "ee"``"ee" in "seek" |
替换构造 Alternation Constructs
替换构造用于修改正则表达式以启用 either/or 匹配。Alternation constructs modify a regular expression to enable either/or matching. 这些构造包括下表中列出的语言元素。These constructs include the language elements listed in the following table. 有关详细信息,请参阅 替换构造。For more information, see Alternation Constructs.
替换构造 Alternation construct | 描述 Description | 模式 Pattern | 匹配 Matches | |||||
---|---|---|---|---|---|---|---|---|
` | ` | 匹配以竖线 (` | ) 字符分隔的任何一个元素。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 指定的正则表达式模式匹配,则匹配 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;否则,匹配可的 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
替换是替换模式中支持的正则表达式语言元素。Substitutions are regular expression language elements that are supported in replacement patterns. 有关更多信息,请参见 替代。For more information, see Substitutions. 下表中列出的元字符是原子零宽度断言。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" |
"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" |
$& |
替换整个匹配项的一个副本。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" |
$_ |
替换整个输入字符串。Substitutes the entire input string. | B+ |
$_ |
"AABBCC" |
"AAAABBCCCC" |
正则表达式选项 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 选项的详细信息,请参阅文章 正则表达式选项。For 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. - 通过使用 分组构造
(?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. |
有关示例,请参阅 正则表达式选项中的 "多行模式" 部分。For an example, see the "Multiline Mode" section in Regular Expression Options. | |
n |
不捕获未命名的组。Do not capture unnamed groups. | 有关示例,请参阅 正则表达式选项中的 "仅显式捕获" 部分。For an example, see the "Explicit Captures Only" section in Regular Expression Options. | |
s |
使用单行模式。Use single-line mode. | 有关示例,请参阅 正则表达式选项中的 "单行模式" 部分。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
其他构造可修改某个正则表达式模式或提供有关该模式的信息。Miscellaneous constructs either modify a regular expression pattern or provide information about it. 下表列出了 .NET 支持的其他构造。The following table lists the miscellaneous constructs supported by .NET. 有关详细信息,请参阅 其他构造。For 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 ) (?# 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 |
原文链接:https://docs.microsoft.com/zh-cn/dotnet/standard/base-types/regular-expression-language-quick-reference?redirectedfrom=MSDN