在 Visual Studio 中使用規則運算式Use regular expressions in Visual Studio

Visual Studio 會使用 .NET 規則運算式來尋找和取代文字。Visual Studio uses .NET regular expressions to find and replace text.

規則運算式範例Regular expression examples

下表包含一些規則運算式字元、運算子、建構及模式的範例。The following table contains some regular expression characters, operators, constructs, and pattern examples. 如需更完整的參考,請參閱規則運算式語言For a more complete reference, see Regular expression language.

用途Purpose 運算式Expression 範例Example
比對任何單一字元 (分行符號除外)。Match any single character (except a line break). 如需詳細資訊,請參閱任何字元For more information, see Any character. 執行個體時提供 SQL Server 登入。. a.o 比對「關於」中的「aro」和「關於」中的「abo」,但不符合「跨」中的「acro」a.o matches "aro" in "around" and "abo" in "about" but not "acro" in "across"
比對先前運算式中零個或多個項目 (比對的字元越多越好)。Match zero or more occurrences of the preceding expression (match as many characters as possible). 如需詳細資訊,請參閱比對零或多次For more information, see Match zero or more times. * a*r 會比對 "rack" 中的 "r"、"ark" 中的 "ar",以及 "aardvark" 中的 "aar"a*r matches "r" in "rack", "ar" in "ark", and "aar" in "aardvark"
比對任何字元零或多次。Match any character zero or more times. .*.* c.*e 會比對 "racket" 中的 "cke"、"comment" 中的 "comme",以及 "code" 中的 "code"c.*e matches "cke" in "racket", "comme" in "comment", and "code" in "code"
比對先前運算式中一個或多個項目 (比對的字元越多越好)。Match one or more occurrences of the preceding expression (match as many characters as possible). 如需詳細資訊,請參閱比對一或多次For more information, see Match one or more times. + e+d 符合「送紙器」中的 "eed" 和「淡出」中的 "ed"e+d matches "eed" in "feeder" and "ed" in "faded"
一或多次比對任何字元。Match any character one or more times. .+.+ e.+e 符合「送紙器」中的「對 feeder」,但在「摘要」中找不到相符專案e.+e matches "eede" in "feeder" but finds no matches in "feed"
比對先前運算式中零個或多個項目 (比對的字元越少越好)。Match zero or more occurrences of the preceding expression (match as few characters as possible). 如需詳細資訊,請參閱比對零或多次 (Lazy (忽略優先) 比對)For more information, see Match zero or more times (lazy match). *?*? \w*?d 符合「褪色」中的 "fad" 和 "ed",而不是整個單字「褪色」,因為延遲相符\w*?d matches "fad" and "ed" in "faded" but not the entire word "faded" due to the lazy match
比對先前運算式中一個或多個項目 (比對的字元越少越好)。Match one or more occurrences of the preceding expression (match as few characters as possible). 如需詳細資訊,請參閱比對一或多次 (Lazy (忽略優先) 比對)For more information, see Match one or more times (lazy match). +?+? e\w+? 比對「休眠」和「ed」中「淡出」的「ee」,但在「淡化」中找不到相符專案e\w+? matches "ee" in "asleep" and "ed" in "faded" but finds no matches in "fade"
將比對字串錨定至行首或字串開頭Anchor the match string to the beginning of a line or string ^ 只有當它出現在行首時,^car 才會符合 "car" 這個字^car matches the word "car" only when it appears at the beginning of a line
將比對字串錨定至行尾Anchor the match string to the end of a line \r?$\r?$ 只有當它出現在行尾時,car\r?$ 才會符合 "car"car\r?$ matches "car" only when it appears at the end of a line
將比對字串錨定至檔案結尾Anchor the match string to the end of the file $ 只有當它出現在檔案結尾時,car$ 才會符合 "car"car$ matches "car" only when it appears at the end of the file
比對集合中的任何單一字元Match any single character in a set [abc][abc] b[abc] 符合 "ba"、"bb" 和 "bc"b[abc] matches "ba", "bb", and "bc"
比對字元範圍之間的任何字元Match any character in a range of characters [a-f][a-f] be[n-t] 符合 "a" 中的 "a"、"ben" (在「底下」)和「旁」中的「be」,但在「以下」中找不到相符專案be[n-t] matches "bet" in "between", "ben" in "beneath", and "bes" in "beside", but finds no matches in "below"
擷取和隱含編號包含在括號內的運算式Capture and implicitly number the expression contained within parenthesis ()() ([a-z])X\1 會比對 "aXa" 和 "bXb",但不比對 "aXb"。([a-z])X\1 matches "aXa"and "bXb", but not "aXb". "\1" 表示第一個運算式群組 "[a-z]"。"\1" refers to the first expression group "[a-z]". 如需詳細資訊,請參閱擷取群組和取代模式For more information, see Capture groups and replacement patterns.
使比對失效Invalidate a match (?!abc)(?!abc) real(?!ity) 會比對 "realty" 和 "really" 中的 "real",但不比對 "reality" 中的 "real"。real(?!ity) matches "real" in "realty" and "really" but not in "reality." 它也會在 "realityreal" 中找到第二個 "real" (但不是第一個 "real")。It also finds the second "real" (but not the first "real") in "realityreal".
比對任何不在一組特定字元中的字元。Match any character that is not in a given set of characters. 如需詳細資訊,請參閱負字元群組For more information, see Negative character group. [^abc][^abc] be[^n-t] 符合 "before"、"beh" 中 "bef" 和 "bel" 中 "" 的 "",但在 "底下" 中找不到相符專案be[^n-t] matches "bef" in "before", "beh" in "behind", and "bel" in "below", but finds no matches in "beneath"
比對符號之前或之後的運算式Match either the expression before or the one after the symbol || (sponge|mud) bath 符合「海綿浴缸」和「mud 浴缸」(sponge|mud) bath matches "sponge bath" and "mud bath"
逸出反斜線之後的字元Escape the character following the backslash \ \^ 符合字元 ^\^ matches the character ^
指定前置字元或群組的出現次數。Specify the number of occurrences of the preceding character or group. 如需詳細資訊,請參閱比對 n 次For more information, see Match exactly n times. {n},其中 'n' 是發生次數{n}, where 'n' is the number of occurrences x(ab){2}x 符合 "對 xababx"x(ab){2}x matches "xababx"
x(ab){2,3}x 比對 "對 xababx" 和 "xabababx",但不符合 "比對 xababababx"x(ab){2,3}x matches "xababx" and "xabababx" but not "xababababx"
比對 Unicode 類別中的文字Match text in a Unicode category. 如需 Unicode 字元類別的詳細資訊,請參閱 Unicode Standard 5.2 字元屬性For more information about Unicode character classes, see Unicode Standard 5.2 Character Properties. \p{X},其中 "X" 是 Unicode 數字。\p{X}, where "X" is the Unicode number. \p{Lu} 符合 "Thomas Doe" 中的 "T" 和 "D"\p{Lu} matches "T" and "D" in "Thomas Doe"
比對字邊界Match a word boundary \b (在字元類別之外 \b 會指定字邊界,在字元類別 \b 內則會指定退格鍵。)\b (Outside a character class \b specifies a word boundary, and inside a character class \b specifies a backspace.) \bin 符合「內部」中的 "in",但在 "pinto" 中找不到相符專案\bin matches "in" in "inside" but finds no matches in "pinto"
比對分行符號 (即歸位字元後面接著新行)Match a line break (that is, a carriage return followed by a new line) \r?\n\r?\n 只有在 "End" 是一行的最後一個字串,且 "Begin" 是下一行的第一個字串時,End\r?\nBegin 才會符合 "End" 和 "Begin"End\r?\nBegin matches "End" and "Begin" only when "End" is the last string in a line and "Begin" is the first string in the next line
比對任何文字字元Match any word character \w\w a\wd 符合 "add" 和 "a1d",而不是 "a d"a\wd matches "add" and "a1d" but not "a d"
比對任何空白字元Match any whitespace character \s\s Public\sInterface 符合「公用介面」一詞Public\sInterface matches the phrase "Public Interface"
比對任何十進位數字字元Match any decimal digit character \d\d \d 符合 "wd40" 中的 "4" 和 "0"\d matches "4" and "0" in "wd40"

@No__t_0,結合一些運算子和結構以符合十六進位數位的範例正則運算式。An example regular expression that combines some of the operators and constructs to match a hexadecimal number is \b0[xX]([0-9a-fA-F]+\)\b. 這個運算式會比對 "對 0xc67f",但不符合 "0xc67g"。This expression matches "0xc67f" but not "0xc67g".

Tip

在 Windows 作業系統中,大部分的程式行都是以 "\r\n" 結尾 (歸位字元後面接著新行)。In Windows operating systems, most lines end in "\r\n" (a carriage return followed by a new line). 這些字元並不可見,但會出現在編輯器中,並傳遞至 .NET 正則運算式服務。These characters aren't visible but are present in the editor and passed to the .NET regular expression service.

擷取群組和取代模式Capture groups and replacement patterns

擷取群組會描寫規則運算式的子運算式,以及擷取輸入字串的子字串。A capture group delineates a subexpression of a regular expression and captures a substring of an input string. 您可以使用規則運算式本身內部 (例如,尋找重複的字詞),或取代模式中擷取的群組。You can use captured groups within the regular expression itself (for example, to look for a repeated word), or in a replacement pattern. 如需詳細資訊,請參閱規則運算式中的群組建構For detailed information, see Grouping constructs in regular expressions.

若要建立編號的擷取群組,請用規則運算式模式中的括弧括住子運算式。To create a numbered capture group, surround the subexpression with parentheses in the regular expression pattern. 擷取會依據規則運算式中的左括號的位置,由左至右自動編號。Captures are numbered automatically from left to right based on the position of the opening parenthesis in the regular expression. 存取擷取的群組:To access the captured group:

  • 正則運算式內:使用 \numberwithin the regular expression: Use \number. 例如,規則運算式 (\w+)\s\1 中的 \1 參考第一個擷取群組 (\w+)For example, \1 in the regular expression (\w+)\s\1 references the first capture group (\w+).

  • 在取代模式中:使用 $numberin a replacement pattern: Use $number. 例如,已分組的規則運算式 (\d)([a-z]) 會定義兩個群組:第一個群組包含單一的十進位數字,第二個群組包含介於 az 之間的單一字元。For example, the grouped regular expression (\d)([a-z]) defines two groups: the first group contains a single decimal digit, and the second group contains a single character between a and z. 運算式在下列字串中找到四個相符項目:1a 2b 3c 4dThe expression finds four matches in the following string: 1a 2b 3c 4d. 取代字串 z$1 只參考第一個群組 ($1),並將字串轉換成 z1 z2 z3 z4The replacement string z$1 references the first group only ($1), and converts the string to z1 z2 z3 z4.

下圖顯示規則運算式 (\w+)\s\1 和取代字串 $1The following image shows a regular expression (\w+)\s\1 and a replacement string $1. 規則運算式和取代模式會參考自動編號為 1 的第一個擷取群組。Both the regular expression and the replacement pattern reference the first capture group that's automatically numbered 1. 當您在 Visual Studio 的 [快速取代] 對話方塊中選擇 [全部取代] 時,會將重複的字組從文字中移除。When you choose Replace all in the Quick Replace dialog box in Visual Studio, repeated words are removed from the text.

Visual Studio 中顯示編號擷取群組的快速取代

Tip

請確定已選取 [快速取代] 對話方塊中的 [使用規則運算式] 按鈕。Make sure the Use Regular Expressions button is selected in the Quick Replace dialog box.

具名的擷取群組Named capture groups

您可以改為為擷取群組命名,不使用自動編號。Instead of relying on the automatic numbering of a capture group, you can give it a name. 具的擷取群組的語法為 (?<name>subexpression)The syntax for a named capture group is (?<name>subexpression).

具名擷取群組 (例如編號的擷取群組),可在規則運算式本身或在取代模式中使用。Named capture groups, like numbered capture groups, can be used within the regular expression itself or in a replacement pattern. 存取具名擷取群組:To access the named capture group:

  • 正則運算式內:使用 \k<name>within the regular expression: Use \k<name>. 例如,規則運算式 (?<repeated>\w+)\s\k<repeated> 中的 \k<repeated> 參考名稱為 repeated 且子運算式為 \w+ 的擷取群組。For example, \k<repeated> in the regular expression (?<repeated>\w+)\s\k<repeated> references the capture group that's named repeated and whose subexpression is \w+.

  • 在取代模式中:使用 ${name}in a replacement pattern: Use ${name}. 例如,${repeated}For example, ${repeated}.

舉例來說,下圖顯示規則運算式 (?<repeated>\w+)\s\k<repeated> 和取代字串 ${repeated}As an example, the following image shows a regular expression (?<repeated>\w+)\s\k<repeated> and a replacement string ${repeated}. 規則運算式和取代模式會參考名為 repeated 的擷取群組。Both the regular expression and the replacement pattern reference the capture group named repeated. 當您在 Visual Studio 的 [快速取代] 對話方塊中選擇 [全部取代] 時,會將重複的字組從文字中移除。When you choose Replace all in the Quick Replace dialog box in Visual Studio, repeated words are removed from the text.

Visual Studio 中顯示具名擷取群組的快速取代

Tip

請確定已選取 [快速取代] 對話方塊中的 [使用規則運算式] 按鈕。Make sure the Use Regular Expressions button is selected in the Quick Replace dialog box.

如需具名擷取群組的詳細資訊,請參閱具名的相符子運算式For more information about named capture groups, see Named matched subexpressions. 如需取代模式中所用規則運算式的詳細資訊,請參閱規則運算式中的替代項目For more information about regular expressions that are used in replacement patterns, see Substitutions in regular expressions.

請參閱See also