群組建構
群組建構會描寫規則運算式 (Regular Expression) 的子運算式 (Subexpression),而且通常會擷取輸入字串的子字串。下表說明規則運算式的群組建構。
群組建構 | 說明 |
---|---|
( 子運算式 ) |
擷取相符的子運算式 (或是非擷取的群組;如需詳細資訊,請參閱規則運算式選項中的 ExplicitCapture 選項)。使用 () 進行擷取會根據左括號的順序自動編號 (從一開始)。第一個擷取 (擷取項目編號零) 是符合整個規則運算式模式的文字。 |
(?< name > 子運算式) |
將符合的子運算式擷取到群組名稱或編號名稱中。用於 name 的字串不可包含任何標點符號,而且不能以數字當做開頭。您可以使用單引號來代替角括弧,例如 |
(?< name1 - name2 > 子運算式) |
(對稱群組定義)。刪除先前定義的 name2 群組的定義,並且在 name1 群組中儲存先前定義的 name2 群組和目前群組之間的間隔。如果沒有定義 name2 群組,比對會回溯。因為刪除 name2 的最後一個定義會顯露 name2 的先前定義,所以這個建構允許 name2 群組的擷取堆疊當做計數器使用,以追蹤巢狀建構 (例如括號)。在這個建構中,name1 是選擇性的。您可以使用單引號來代替角括弧,例如 如需詳細資訊,請參閱本主題內的範例。 |
(?: 子運算式) |
(非擷取的群組)。不會擷取與子運算式相符的子字串。 |
(?imnsx-imnsx: 子運算式) |
套用或停用子運算式內的指定選項。例如, |
(?= 子運算式) |
(無寬度右合樣 (Positive Lookahead) 判斷提示 (Assertion))。只有在子運算式於右邊的這個位置進行比對時,才繼續比對。例如, |
(?! 子運算式) |
(無寬度右不合樣 (Negative Lookahead) 判斷提示)。只有在子運算式不於右邊的這個位置進行比對時,才繼續比對。例如, |
(?<= 子運算式) |
(無寬度左合樣 (Positive Lookbehind) 判斷提示)。只有在子運算式於左邊的這個位置進行比對時,才繼續比對。例如, |
(?<! 子運算式) |
(無寬度左不合樣 (Negative Lookbehind) 判斷提示)。只有在子運算式不於左邊的位置進行比對時,才繼續比對。 |
(?> 子運算式) |
(非回溯子運算式,也稱為窮盡 (Greedy) 子運算式)。子運算式會完全比對一次,然後不參與逐步的回溯 (也就是說,子運算式只會比對由子運算式單獨比對的字串)。 根據預設,如果比對不成功,則回溯搜尋其他可能相符的字串。如果您知道回溯不會成功,便可以使用非回溯的子運算式來避免不必要的搜尋,以提升效能。 |
已命名的擷取是根據左括號由左至右的順序 (類似未命名的擷取) 依序編號,但是已命名的擷取之編號是在所有未命名的擷取已經計算之後才開始編號。例如,模式 ((?<One>abc)/d+)?(?<Two>xyz)(.*)
會依編號和名稱產生下列擷取群組。(第一個擷取 (編號 0) 永遠參考整個模式)。
編號 | 名稱 | 模式 |
---|---|---|
0 |
0 (預設名稱) |
|
1 |
1 (預設名稱) |
|
2 |
2 (預設名稱) |
|
3 |
One |
|
4 |
Two |
|
對稱群組定義範例
下列程式碼範例將示範使用對稱群組定義來比對輸入字串內的左右角括弧 (<>) 的方法。此範例中 Open 和 Close 群組的擷取集合會以類似堆疊的方式來使用,以追蹤相符的成對角括弧:將每一個擷取到的左角括弧推入 Open 群組的擷取集合內、將每一個擷取到的右角括弧推入 Close 群組的擷取集合中,然後對稱群組定義會確定每個左角括弧都有相符的右角括弧。
' This code example demonstrates using the balancing group definition feature of
' regular expressions to match balanced left angle bracket (<) and right angle
' bracket (>) characters in a string.
Imports System
Imports System.Text.RegularExpressions
Class Sample
Public Shared Sub Main()
'
' The following expression matches all balanced left and right angle brackets(<>).
' The expression:
' 1) Matches and discards zero or more non-angle bracket characters.
' 2) Matches zero or more of:
' 2a) One or more of:
' 2a1) A group named "Open" that matches a left angle bracket, followed by zero
' or more non-angle bracket characters.
' "Open" essentially counts the number of left angle brackets.
' 2b) One or more of:
' 2b1) A balancing group named "Close" that matches a right angle bracket,
' followed by zero or more non-angle bracket characters.
' "Close" essentially counts the number of right angle brackets.
' 3) If the "Open" group contains an unaccounted for left angle bracket, the
' entire regular expression fails.
'
Dim pattern As String = "^[^<>]*" & _
"(" + "((?'Open'<)[^<>]*)+" & _
"((?'Close-Open'>)[^<>]*)+" + ")*" & _
"(?(Open)(?!))$"
Dim input As String = "<abc><mno<xyz>>"
'
Dim m As Match = Regex.Match(input, pattern)
If m.Success = True Then
Console.WriteLine("Input: ""{0}"" " & vbCrLf & "Match: ""{1}""", _
input, m)
Else
Console.WriteLine("Match failed.")
End If
End Sub 'Main
End Class 'Sample
'This code example produces the following results:
'
'Input: "<abc><mno<xyz>>"
'Match: "<abc><mno<xyz>>"
'
// This code example demonstrates using the balancing group definition feature of
// regular expressions to match balanced left angle bracket (<) and right angle
// bracket (>) characters in a string.
using System;
using System.Text.RegularExpressions;
class Sample
{
public static void Main()
{
/*
The following expression matches all balanced left and right angle brackets(<>).
The expression:
1) Matches and discards zero or more non-angle bracket characters.
2) Matches zero or more of:
2a) One or more of:
2a1) A group named "Open" that matches a left angle bracket, followed by zero
or more non-angle bracket characters.
"Open" essentially counts the number of left angle brackets.
2b) One or more of:
2b1) A balancing group named "Close" that matches a right angle bracket,
followed by zero or more non-angle bracket characters.
"Close" essentially counts the number of right angle brackets.
3) If the "Open" group contains an unaccounted for left angle bracket, the
entire regular expression fails.
*/
string pattern = "^[^<>]*" +
"(" +
"((?'Open'<)[^<>]*)+" +
"((?'Close-Open'>)[^<>]*)+" +
")*" +
"(?(Open)(?!))$";
string input = "<abc><mno<xyz>>";
//
Match m = Regex.Match(input, pattern);
if (m.Success == true)
Console.WriteLine("Input: \"{0}\" \nMatch: \"{1}\"", input, m);
else
Console.WriteLine("Match failed.");
}
}
/*
This code example produces the following results:
Input: "<abc><mno<xyz>>"
Match: "<abc><mno<xyz>>"
*/