規則運算式選項Regular Expression Options

依預設,輸入字串與規則運算式模式中任何常值字元的比較會區分大小寫,規則運算式模式中的空白字元會解譯成常值空白字元,而規則運算式中的擷取群組會隱含也會明確命名。By default, the comparison of an input string with any literal characters in a regular expression pattern is case sensitive, white space in a regular expression pattern is interpreted as literal white-space characters, and capturing groups in a regular expression are named implicitly as well as explicitly. 您可以藉由指定規則運算式選項來修改這些預設規則運算式行為和幾個其他方面。You can modify these and several other aspects of default regular expression behavior by specifying regular expression options. 這些選項 (列示於下表) 可以內嵌為規則運算式模式的部分,或是提供給 System.Text.RegularExpressions.Regex 類別建構函式或靜態模式比對方法,以做為 System.Text.RegularExpressions.RegexOptions 列舉值。These options, which are listed in the following table, can be included inline as part of the regular expression pattern, or they can be supplied to a System.Text.RegularExpressions.Regex class constructor or static pattern matching method as a System.Text.RegularExpressions.RegexOptions enumeration value.

RegexOptions 成員RegexOptions member 內嵌字元Inline character 作用Effect
None 無法使用Not available 使用預設行為。Use default behavior. 如需詳細資訊,請參閱預設選項For more information, see Default Options.
IgnoreCase i 使用不區分大小寫的比對方式。Use case-insensitive matching. 如需詳細資訊,請參閱不區分大小寫比對For more information, see Case-Insensitive Matching.
Multiline m 使用多行模式,其中 ^$ 會比對每一行的開頭與結尾 (而不是輸入字串的開頭和結尾)。Use multiline mode, where ^ and $ match the beginning and end of each line (instead of the beginning and end of the input string). 如需詳細資訊,請參閱多行模式For more information, see Multiline Mode.
Singleline s 使用單行模式,其中句點 (.) 會比對每個字元 (而不是 \n 以外的每個字元)。Use single-line mode, where the period (.) matches every character (instead of every character except \n). 如需詳細資訊,請參閱單行模式For more information, see Single-line Mode.
ExplicitCapture n 不擷取未命名的群組。Do not capture unnamed groups. 唯一有效的擷取是明確命名或編號的群組,格式如下:(?<name> subexpression)The only valid captures are explicitly named or numbered groups of the form (?<name> subexpression). 如需詳細資訊,請參閱僅明確擷取For more information, see Explicit Captures Only.
Compiled 無法使用Not available 將規則運算式編譯為組件。Compile the regular expression to an assembly. 如需詳細資訊,請參閱編譯的規則運算式For more information, see Compiled Regular Expressions.
IgnorePatternWhitespace x 在模式中排除未逸出的空白字元,並且在數字符號 (#) 後面啟用註解。Exclude unescaped white space from the pattern, and enable comments after a number sign (#). 如需詳細資訊,請參閱忽略空白字元For more information, see Ignore White Space.
RightToLeft 無法使用Not available 變更搜尋方向。Change the search direction. 搜尋方向為由右至左,而不是由左至右。Search moves from right to left instead of from left to right. 如需詳細資訊,請參閱由右至左模式For more information, see Right-to-Left Mode.
ECMAScript 無法使用Not available 為運算式啟用符合 ECMAScript 規範的行為。Enable ECMAScript-compliant behavior for the expression. 如需詳細資訊,請參閱 ECMAScript 相符行為For more information, see ECMAScript Matching Behavior.
CultureInvariant 無法使用Not available 忽略語言中的文化特性差異。Ignore cultural differences in language. 如需詳細資訊,請參閱使用不因國別而異的文化特性比較For more information, see Comparison Using the Invariant Culture.

指定選項Specifying the Options

指定規則運算式選項的方式有三種:You can specify options for regular expressions in one of three ways:

  • options 類別建構函式或靜態 (在 Visual Basic 中為 System.Text.RegularExpressions.Regex) 模式比對方法 (例如 SharedRegex.Regex(String, RegexOptions)) 的 Regex.Match(String, String, RegexOptions) 參數中。In the options parameter of a System.Text.RegularExpressions.Regex class constructor or static (Shared in Visual Basic) pattern-matching method, such as Regex.Regex(String, RegexOptions) or Regex.Match(String, String, RegexOptions). options 參數是 System.Text.RegularExpressions.RegexOptions 列舉值的位元「或」組合。The options parameter is a bitwise OR combination of System.Text.RegularExpressions.RegexOptions enumerated values.

    當使用類別建構函式的 options 參數提供選項給 Regex 執行個體時,選項會指派給 System.Text.RegularExpressions.RegexOptions 屬性。When options are supplied to a Regex instance by using the options parameter of a class constructor, the options are assigned to the System.Text.RegularExpressions.RegexOptions property. 不過,System.Text.RegularExpressions.RegexOptions 屬性不會在規則運算式模式中反映內嵌選項。However, the System.Text.RegularExpressions.RegexOptions property does not reflect inline options in the regular expression pattern itself.

    下列範例提供一個實例。The following example provides an illustration. 其使用 options 方法的 Regex.Match(String, String, RegexOptions) 參數來啟用不區分大小寫比對,並且在識別以字母 "d" 開頭的文字時,忽略模式空白字元。It uses the options parameter of the Regex.Match(String, String, RegexOptions) method to enable case-insensitive matching and to ignore pattern white space when identifying words that begin with the letter "d".

    string pattern = @"d \w+ \s";
    string input = "Dogs are decidedly good pets.";
    RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace;
    
    foreach (Match match in Regex.Matches(input, pattern, options))
       Console.WriteLine("'{0}// found at index {1}.", match.Value, match.Index);
    // The example displays the following output:
    //    'Dogs // found at index 0.
    //    'decidedly // found at index 9.      
    
    Dim pattern As String = "d \w+ \s"
    Dim input As String = "Dogs are decidedly good pets."
    Dim options As RegexOptions = RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace
    
    For Each match As Match In Regex.Matches(input, pattern, options)
       Console.WriteLine("'{0}' found at index {1}.", match.Value, match.Index)
    Next
    ' The example displays the following output:
    '    'Dogs ' found at index 0.
    '    'decidedly ' found at index 9.      
    
  • 使用語法 (?imnsx-imnsx),在規則運算式模式中套用內嵌選項。By applying inline options in a regular expression pattern with the syntax (?imnsx-imnsx). 此選項會從定義選項的位置開始套用至模式,直到模式結尾,或是有其他內嵌選項取消定義選項為止。The option applies to the pattern from the point that the option is defined to either the end of the pattern or to the point at which the option is undefined by another inline option. 請注意,Regex 執行個體的 System.Text.RegularExpressions.RegexOptions 屬性不會反映這些內嵌選項。Note that the System.Text.RegularExpressions.RegexOptions property of a Regex instance does not reflect these inline options. 如需詳細資訊,請參閱其他建構主題。For more information, see the Miscellaneous Constructs topic.

    下列範例提供一個實例。The following example provides an illustration. 其使用內嵌選項來啟用不區分大小寫比對,並且在識別以字母 "d" 開頭的文字時,忽略模式空白字元。It uses inline options to enable case-insensitive matching and to ignore pattern white space when identifying words that begin with the letter "d".

    string pattern = @"(?ix) d \w+ \s";
    string input = "Dogs are decidedly good pets.";
    
    foreach (Match match in Regex.Matches(input, pattern))
       Console.WriteLine("'{0}// found at index {1}.", match.Value, match.Index);
    // The example displays the following output:
    //    'Dogs // found at index 0.
    //    'decidedly // found at index 9.      
    
    Dim pattern As String = "\b(?ix) d \w+ \s"
    Dim input As String = "Dogs are decidedly good pets."
    
    For Each match As Match In Regex.Matches(input, pattern)
       Console.WriteLine("'{0}' found at index {1}.", match.Value, match.Index)
    Next
    ' The example displays the following output:
    '    'Dogs ' found at index 0.
    '    'decidedly ' found at index 9.      
    
  • 使用語法 (?imnsx-imnsx:subexpression),在規則運算式模式的特定群組建構中套用內嵌選項。By applying inline options in a particular grouping construct in a regular expression pattern with the syntax (?imnsx-imnsx:subexpression). 如果選項集前面沒有符號,會開啟選項集;如果選項集前面有減號,則會關閉選項集。No sign before a set of options turns the set on; a minus sign before a set of options turns the set off. ? 是語言結構的固定部分,無論啟用或停用選項,都需要此語法。)此選項只適用于該群組。(? is a fixed part of the language construct's syntax that is required whether options are enabled or disabled.) The option applies only to that group. 如需詳細資訊,請參閱分組建構For more information, see Grouping Constructs.

    下列範例提供一個實例。The following example provides an illustration. 其使用群組建構中的內嵌選項來啟用不區分大小寫比對,並且在識別以字母 "d" 開頭的文字時,忽略模式空白字元。It uses inline options in a grouping construct to enable case-insensitive matching and to ignore pattern white space when identifying words that begin with the letter "d".

    string pattern = @"\b(?ix: d \w+)\s";
    string input = "Dogs are decidedly good pets.";
    
    foreach (Match match in Regex.Matches(input, pattern))
       Console.WriteLine("'{0}// found at index {1}.", match.Value, match.Index);
    // The example displays the following output:
    //    'Dogs // found at index 0.
    //    'decidedly // found at index 9.      
    
    Dim pattern As String = "\b(?ix: d \w+)\s"
    Dim input As String = "Dogs are decidedly good pets."
    
    For Each match As Match In Regex.Matches(input, pattern)
       Console.WriteLine("'{0}' found at index {1}.", match.Value, match.Index)
    Next
    ' The example displays the following output:
    '    'Dogs ' found at index 0.
    '    'decidedly ' found at index 9.      
    

如果將選項指定為內嵌,選項或選項集前面的減號 (-) 會關閉那些選項。If options are specified inline, a minus sign (-) before an option or set of options turns off those options. 例如,內嵌建構 (?ix-ms) 會開啟 RegexOptions.IgnoreCaseRegexOptions.IgnorePatternWhitespace 選項,並關閉 RegexOptions.MultilineRegexOptions.Singleline 選項。For example, the inline construct (?ix-ms) turns on the RegexOptions.IgnoreCase and RegexOptions.IgnorePatternWhitespace options and turns off the RegexOptions.Multiline and RegexOptions.Singleline options. 依預設,會關閉所有規則運算式選項。All regular expression options are turned off by default.

注意

如果建構函式或方法呼叫的 options 參數指定的規則運算式選項,與規則運算式模式中指定內嵌的選項相衝突,則會使用內嵌選項。If the regular expression options specified in the options parameter of a constructor or method call conflict with the options specified inline in a regular expression pattern, the inline options are used.

選項參數和內嵌都可以用來設定下列五個規則運算式選項:The following five regular expression options can be set both with the options parameter and inline:

下列五個規則運算式選項可以用 options 參數來設定,但不能設定內嵌:The following five regular expression options can be set using the options parameter but cannot be set inline:

決定選項Determining the Options

您可以判定,在擷取唯讀 Regex 屬性的值來將物件具現化時,提供了哪些選項給 Regex.Options 物件。You can determine which options were provided to a Regex object when it was instantiated by retrieving the value of the read-only Regex.Options property. 在判定為 Regex.CompileToAssembly 方法建立之編譯規則運算式定義的選項時,此屬性特別好用。This property is particularly useful for determining the options that are defined for a compiled regular expression created by the Regex.CompileToAssembly method.

若要測試任何選項 (RegexOptions.None 除外) 是否存在,請使用 Regex.Options 屬性的值和您感興趣的 RegexOptions 值來執行 AND 作業。To test for the presence of any option except RegexOptions.None, perform an AND operation with the value of the Regex.Options property and the RegexOptions value in which you are interested. 然後測試結果是否等於 RegexOptions 值。Then test whether the result equals that RegexOptions value. 下列範例會測試是否已設定 RegexOptions.IgnoreCase 選項。The following example tests whether the RegexOptions.IgnoreCase option has been set.

if ((rgx.Options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase)
   Console.WriteLine("Case-insensitive pattern comparison.");
else
   Console.WriteLine("Case-sensitive pattern comparison.");
If (rgx.Options And RegexOptions.IgnoreCase) = RegexOptions.IgnoreCase Then
   Console.WriteLine("Case-insensitive pattern comparison.")
Else
   Console.WriteLine("Case-sensitive pattern comparison.")
End If   

若要測試 RegexOptions.None,請判定 Regex.Options 屬性的值是否等於 RegexOptions.None,如下列範例所示。To test for RegexOptions.None, determine whether the value of the Regex.Options property is equal to RegexOptions.None, as the following example illustrates.

if (rgx.Options == RegexOptions.None)
   Console.WriteLine("No options have been set.");
If rgx.Options = RegexOptions.None Then
   Console.WriteLine("No options have been set.")
End If

以下各節會列出 .NET 中規則運算式所支援的選項。The following sections list the options supported by regular expression in .NET.

預設選項Default Options

RegexOptions.None 選項指出未指定任何選項,而規則運算式引擎使用其預設行為。The RegexOptions.None option indicates that no options have been specified, and the regular expression engine uses its default behavior. 其中包括下列項目:This includes the following:

  • 模式被解譯為標準規則運算式,而不是 ECMAScript 規則運算式。The pattern is interpreted as a canonical rather than an ECMAScript regular expression.

  • 在輸入字串中,由左至右比對規則運算式模式。The regular expression pattern is matched in the input string from left to right.

  • 比較會區分大小寫。Comparisons are case-sensitive.

  • ^$ 語言項目會比對輸入字串的開頭和結尾。The ^ and $ language elements match the beginning and end of the input string.

  • . 語言項目會比對 \n 以外的每個字元。The . language element matches every character except \n.

  • 規則運算式模式中的任何空白字元會被解譯成常值空白字元。Any white space in a regular expression pattern is interpreted as a literal space character.

  • 將模式與輸入字串比較時,會使用目前文化特性的慣例。The conventions of the current culture are used when comparing the pattern to the input string.

  • 規則運算式模式中的擷取群組是隱含的,也是明確的。Capturing groups in the regular expression pattern are implicit as well as explicit.

注意

RegexOptions.None 選項沒有內嵌對等項目。The RegexOptions.None option has no inline equivalent. 將規則運算式選項套用為內嵌時,會關閉特定選項,依據各選項逐一還原預設行為。When regular expression options are applied inline, the default behavior is restored on an option-by-option basis, by turning a particular option off. 例如,(?i) 會開啟不區分大小寫比較,而 (?-i) 會還原預設區分大小寫比較。For example, (?i) turns on case-insensitive comparison, and (?-i) restores the default case-sensitive comparison.

因為 RegexOptions.None 選項代表規則運算式引擎的預設行為,所以很少明確地指定在方法呼叫中,Because the RegexOptions.None option represents the default behavior of the regular expression engine, it is rarely explicitly specified in a method call. 而是會呼叫不含 options 參數的建構函式或靜態模式比對方法。A constructor or static pattern-matching method without an options parameter is called instead.

不區分大小寫比對Case-Insensitive Matching

IgnoreCase 選項 (或 i 內嵌選項) 提供不區分大小寫比對。The IgnoreCase option, or the i inline option, provides case-insensitive matching. 依預設,會使用目前文化特性的大小寫慣例。By default, the casing conventions of the current culture are used.

下列範例定義規則運算式模式 \bthe\w*\b,它會比對以 "the" 開頭的所有文字。The following example defines a regular expression pattern, \bthe\w*\b, that matches all words starting with "the". 因為第一次呼叫 Match 方法是使用預設的區分大小寫比較,所以輸出指出未比對句子開頭的 "The" 字串。Because the first call to the Match method uses the default case-sensitive comparison, the output indicates that the string "The" that begins the sentence is not matched. 選項設為 Match 來呼叫 IgnoreCase 方法時,才會加以比對。It is matched when the Match method is called with options set to IgnoreCase.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\bthe\w*\b";
      string input = "The man then told them about that event.";
      foreach (Match match in Regex.Matches(input, pattern))
         Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index);

      Console.WriteLine();
      foreach (Match match in Regex.Matches(input, pattern, 
                                            RegexOptions.IgnoreCase))
         Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index);
   }
}
// The example displays the following output:
//       Found then at index 8.
//       Found them at index 18.
//       
//       Found The at index 0.
//       Found then at index 8.
//       Found them at index 18.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\bthe\w*\b"
      Dim input As String = "The man then told them about that event."
      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index)
      Next
      Console.WriteLine()
      For Each match As Match In Regex.Matches(input, pattern, _
                                               RegexOptions.IgnoreCase)
         Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index)
      Next
   End Sub
End Module
' The example displays the following output:
'       Found then at index 8.
'       Found them at index 18.
'       
'       Found The at index 0.
'       Found then at index 8.
'       Found them at index 18.

下列範例會修改上一個範例中的規則運算式模式,改為使用內嵌選項,而不是使用 options 參數,以提供不區分大小寫比較。The following example modifies the regular expression pattern from the previous example to use inline options instead of the options parameter to provide case-insensitive comparison. 第一個模式在群組建構中定義不區分大小寫選項,只套用於字串 "the" 中的字母 "t"。The first pattern defines the case-insensitive option in a grouping construct that applies only to the letter "t" in the string "the". 因為選項建構出現在模式開頭,所以第二個模式會將不區分大小寫選項套用於整個規則運算式。Because the option construct occurs at the beginning of the pattern, the second pattern applies the case-insensitive option to the entire regular expression.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b(?i:t)he\w*\b";
      string input = "The man then told them about that event.";
      foreach (Match match in Regex.Matches(input, pattern))
         Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index);

      Console.WriteLine();
      pattern = @"(?i)\bthe\w*\b";
      foreach (Match match in Regex.Matches(input, pattern, 
                                            RegexOptions.IgnoreCase))
         Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index);
   }
}
// The example displays the following output:
//       Found The at index 0.
//       Found then at index 8.
//       Found them at index 18.
//       
//       Found The at index 0.
//       Found then at index 8.
//       Found them at index 18.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b(?i:t)he\w*\b"
      Dim input As String = "The man then told them about that event."
      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index)
      Next
      Console.WriteLine()
      pattern = "(?i)\bthe\w*\b"
      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index)
      Next
   End Sub
End Module
' The example displays the following output:
'       Found The at index 0.
'       Found then at index 8.
'       Found them at index 18.
'       
'       Found The at index 0.
'       Found then at index 8.
'       Found them at index 18.

多行模式Multiline Mode

RegexOptions.Multiline 選項 (或 m 內嵌選項) 可讓規則運算式引擎處理構成多行的輸入字串。The RegexOptions.Multiline option, or the m inline option, enables the regular expression engine to handle an input string that consists of multiple lines. 它會變更 ^$ 語言項目的解譯,以便比對字行的開頭和結尾,而不是輸入字串的開頭和結尾。It changes the interpretation of the ^ and $ language elements so that they match the beginning and end of a line, instead of the beginning and end of the input string.

依預設,$ 只會比對輸入字串的結尾。By default, $ matches only the end of the input string. 如果您指定 RegexOptions.Multiline 選項,則會比對新行字元 (\n) 或輸入字串的結尾。If you specify the RegexOptions.Multiline option, it matches either the newline character (\n) or the end of the input string. 不過,並不會比對歸位字元/換行字元組合。It does not, however, match the carriage return/line feed character combination. 若要順利比對,請使用子運算式 \r?$,而不只是使用 $To successfully match them, use the subexpression \r?$ instead of just $.

下列範例會擷取保齡球員的名字和分數,並將其加入 SortedList<TKey,TValue> 集合,以遞減順序排序。The following example extracts bowlers' names and scores and adds them to a SortedList<TKey,TValue> collection that sorts them in descending order. 呼叫 Matches 方法兩次。The Matches method is called twice. 在第一次呼叫方法時,規則運算式為 ^(\w+)\s(\d+)$,且沒有設定選項。In the first method call, the regular expression is ^(\w+)\s(\d+)$ and no options are set. 如輸出所示,因為規則運算式引擎無法隨著輸入字串的開頭和結尾來比對輸入模式,所以沒有找到相符項目。As the output shows, because the regular expression engine cannot match the input pattern along with the beginning and end of the input string, no matches are found. 在第二次呼叫方法時,規則運算式變更為 ^(\w+)\s(\d+)\r?$,且選項設為 RegexOptions.MultilineIn the second method call, the regular expression is changed to ^(\w+)\s(\d+)\r?$ and the options are set to RegexOptions.Multiline. 如輸出所示,已成功比對名字和分數,且分數以遞減順序顯示。As the output shows, the names and scores are successfully matched, and the scores are displayed in descending order.

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      SortedList<int, string> scores = new SortedList<int, string>(new DescendingComparer<int>());
      
      string input = "Joe 164\n" + 
                     "Sam 208\n" + 
                     "Allison 211\n" + 
                     "Gwen 171\n"; 
      string pattern = @"^(\w+)\s(\d+)$";
      bool matched = false;
      
      Console.WriteLine("Without Multiline option:");
      foreach (Match match in Regex.Matches(input, pattern))
      {
         scores.Add(Int32.Parse(match.Groups[2].Value), (string) match.Groups[1].Value);
         matched = true;
      }
      if (! matched)
         Console.WriteLine("   No matches.");
      Console.WriteLine();

      // Redefine pattern to handle multiple lines.
      pattern = @"^(\w+)\s(\d+)\r*$";
      Console.WriteLine("With multiline option:");
      foreach (Match match in Regex.Matches(input, pattern, RegexOptions.Multiline))
         scores.Add(Int32.Parse(match.Groups[2].Value), (string) match.Groups[1].Value);

      // List scores in descending order. 
      foreach (KeyValuePair<int, string> score in scores)
         Console.WriteLine("{0}: {1}", score.Value, score.Key);
   }
}

public class DescendingComparer<T> : IComparer<T>
{
   public int Compare(T x, T y)
   {
      return Comparer<T>.Default.Compare(x, y) * -1;       
   }
}
// The example displays the following output:
//   Without Multiline option:
//      No matches.
//   
//   With multiline option:
//   Allison: 211
//   Sam: 208
//   Gwen: 171
//   Joe: 164
Imports System.Collections.Generic
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim scores As New SortedList(Of Integer, String)(New DescendingComparer(Of Integer)())
      
      Dim input As String = "Joe 164" + vbCrLf + _
                            "Sam 208" + vbCrLf + _
                            "Allison 211" + vbCrLf + _
                            "Gwen 171" + vbCrLf
      Dim pattern As String = "^(\w+)\s(\d+)$"
      Dim matched As Boolean = False
      
      Console.WriteLine("Without Multiline option:")
      For Each match As Match In Regex.Matches(input, pattern)
         scores.Add(CInt(match.Groups(2).Value), match.Groups(1).Value)
         matched = True
      Next
      If Not matched Then Console.WriteLine("   No matches.")
      Console.WriteLine()

      ' Redefine pattern to handle multiple lines.
      pattern = "^(\w+)\s(\d+)\r*$"
      Console.WriteLine("With multiline option:")
      For Each match As Match In Regex.Matches(input, pattern, RegexOptions.Multiline)
         scores.Add(CInt(match.Groups(2).Value), match.Groups(1).Value)
      Next
      ' List scores in descending order. 
      For Each score As KeyValuePair(Of Integer, String) In scores
         Console.WriteLine("{0}: {1}", score.Value, score.Key)
      Next
   End Sub
End Module

Public Class DescendingComparer(Of T) : Implements IComparer(Of T)
   Public Function Compare(x As T, y As T) As Integer _
          Implements IComparer(Of T).Compare
      Return Comparer(Of T).Default.Compare(x, y) * -1       
   End Function
End Class
' The example displays the following output:
'    Without Multiline option:
'       No matches.
'    
'    With multiline option:
'    Allison: 211
'    Sam: 208
'    Gwen: 171
'    Joe: 164

規則運算式模式 ^(\w+)\s(\d+)\r*$ 的定義如下表所示。The regular expression pattern ^(\w+)\s(\d+)\r*$ is defined as shown in the following table.

模式Pattern 描述Description
^ 從字行開頭開始。Begin at the start of the line.
(\w+) 比對一個或多個文字字元。Match one or more word characters. 這是第一個擷取群組。This is the first capturing group.
\s 比對空白字元。Match a white-space character.
(\d+) 比對一個或多個十進位數字。Match one or more decimal digits. 這是第二個擷取群組。This is the second capturing group.
\r? 比對零或一個歸位字元。Match zero or one carriage return character.
$ 在字行結尾結束。End at the end of the line.

下列範例與上一個範例相同,只是下列範例是使用內嵌選項 (?m) 來設定多行選項。The following example is equivalent to the previous one, except that it uses the inline option (?m) to set the multiline option.

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      SortedList<int, string> scores = new SortedList<int, string>(new DescendingComparer<int>());
      
      string input = "Joe 164\n" +  
                     "Sam 208\n" +  
                     "Allison 211\n" +  
                     "Gwen 171\n"; 
      string pattern = @"(?m)^(\w+)\s(\d+)\r*$";

      foreach (Match match in Regex.Matches(input, pattern, RegexOptions.Multiline))
         scores.Add(Convert.ToInt32(match.Groups[2].Value), match.Groups[1].Value);

      // List scores in descending order. 
      foreach (KeyValuePair<int, string> score in scores)
         Console.WriteLine("{0}: {1}", score.Value, score.Key);
   }
}

public class DescendingComparer<T> : IComparer<T>
{
   public int Compare(T x, T y) 
   {
      return Comparer<T>.Default.Compare(x, y) * -1;       
   }
}
// The example displays the following output:
//    Allison: 211
//    Sam: 208
//    Gwen: 171
//    Joe: 164
Imports System.Collections.Generic
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim scores As New SortedList(Of Integer, String)(New DescendingComparer(Of Integer)())
      
      Dim input As String = "Joe 164" + vbCrLf + _
                            "Sam 208" + vbCrLf + _
                            "Allison 211" + vbCrLf + _
                            "Gwen 171" + vbCrLf
      Dim pattern As String = "(?m)^(\w+)\s(\d+)\r*$"

      For Each match As Match In Regex.Matches(input, pattern, RegexOptions.Multiline)
         scores.Add(CInt(match.Groups(2).Value), match.Groups(1).Value)
      Next
      ' List scores in descending order. 
      For Each score As KeyValuePair(Of Integer, String) In scores
         Console.WriteLine("{0}: {1}", score.Value, score.Key)
      Next
   End Sub
End Module

Public Class DescendingComparer(Of T) : Implements IComparer(Of T)
   Public Function Compare(x As T, y As T) As Integer _
          Implements IComparer(Of T).Compare
      Return Comparer(Of T).Default.Compare(x, y) * -1       
   End Function
End Class
' The example displays the following output:
'    Allison: 211
'    Sam: 208
'    Gwen: 171
'    Joe: 164

單行模式Single-line Mode

RegexOptions.Singleline 選項 (或 s 內嵌選項) 會使規則運算式引擎將輸入字串當作其包含單行。The RegexOptions.Singleline option, or the s inline option, causes the regular expression engine to treat the input string as if it consists of a single line. 其作法是變更句點 (.) 語言項目的行為,使其比對每個字元,而不是比對新行字元 \n 或 \u000A 以外的每個字元。It does this by changing the behavior of the period (.) language element so that it matches every character, instead of matching every character except for the newline character \n or \u000A.

下列範例說明,當您使用 . 選項時,RegexOptions.Singleline 語言項目的行為有何變更。The following example illustrates how the behavior of the . language element changes when you use the RegexOptions.Singleline option. 規則運算式 ^.+ 會從字串開頭開始,比對每一個字元。The regular expression ^.+ starts at the beginning of the string and matches every character. 根據預設,比對會在第一行結尾結束。規則運算式模式會比對歸位字元 \r 或 \u000D,但不會比對 \nBy default, the match ends at the end of the first line; the regular expression pattern matches the carriage return character, \r or \u000D, but it does not match \n. 由於 RegexOptions.Singleline 選項會將整個輸入字串解譯為單行,因此它會比對輸入字串中的每個字元,包括 \nBecause the RegexOptions.Singleline option interprets the entire input string as a single line, it matches every character in the input string, including \n.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "^.+";
      string input = "This is one line and" + Environment.NewLine + "this is the second.";
      foreach (Match match in Regex.Matches(input, pattern))
         Console.WriteLine(Regex.Escape(match.Value));

      Console.WriteLine();
      foreach (Match match in Regex.Matches(input, pattern, RegexOptions.Singleline))
         Console.WriteLine(Regex.Escape(match.Value));
   }
}
// The example displays the following output:
//       This\ is\ one\ line\ and\r
//       
//       This\ is\ one\ line\ and\r\nthis\ is\ the\ second\.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "^.+"
      Dim input As String = "This is one line and" + vbCrLf + "this is the second."
      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine(Regex.Escape(match.Value))
      Next
      Console.WriteLine()
      For Each match As Match In Regex.Matches(input, pattern, RegexOptions.SingleLine)
         Console.WriteLine(Regex.Escape(match.Value))
      Next
   End Sub
End Module
' The example displays the following output:
'       This\ is\ one\ line\ and\r
'       
'       This\ is\ one\ line\ and\r\nthis\ is\ the\ second\.

下列範例與上一個範例相同,只是下列範例是使用內嵌選項 (?s) 來啟用單行模式。The following example is equivalent to the previous one, except that it uses the inline option (?s) to enable single-line mode.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {      
      string pattern = "(?s)^.+";
      string input = "This is one line and" + Environment.NewLine + "this is the second.";

      foreach (Match match in Regex.Matches(input, pattern))
         Console.WriteLine(Regex.Escape(match.Value));
   }
}
// The example displays the following output:
//       This\ is\ one\ line\ and\r\nthis\ is\ the\ second\.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "(?s)^.+"
      Dim input As String = "This is one line and" + vbCrLf + "this is the second."

      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine(Regex.Escape(match.Value))
      Next
   End Sub
End Module
' The example displays the following output:
'       This\ is\ one\ line\ and\r\nthis\ is\ the\ second\.

僅明確擷取Explicit Captures Only

依預設,擷取群組的定義方式是在規則運算式模式中使用括號。By default, capturing groups are defined by the use of parentheses in the regular expression pattern. 具名群組是以 (?<name>subexpression) 語言選項來指派名稱或號碼,而未具名群組可透過索引來存取。Named groups are assigned a name or number by the (?<name>subexpression) language option, whereas unnamed groups are accessible by index. GroupCollection 物件中,未具名群組的前面是具名群組。In the GroupCollection object, unnamed groups precede named groups.

群組建構通常只用來將數量詞套用至多個語言項目,我們對所擷取的子字串並不感興趣。Grouping constructs are often used only to apply quantifiers to multiple language elements, and the captured substrings are of no interest. 例如,如果下列運算式:For example, if the following regular expression:

\b\(?((\w+),?\s?)+[\.!?]\)?

目的只是要從文件中擷取以句點、驚嘆號或問號結尾的句子,則我們只對所產生的句子 (由 Match 物件代表) 感興趣。is intended only to extract sentences that end with a period, exclamation point, or question mark from a document, only the resulting sentence (which is represented by the Match object) is of interest. 我們對集合中的個別文字並不感興趣。The individual words in the collection are not.

非後續使用的擷取群組可能會耗用很多資源,因為規則運算式引擎必須同時填入 GroupCollectionCaptureCollection 集合物件。Capturing groups that are not subsequently used can be expensive, because the regular expression engine must populate both the GroupCollection and CaptureCollection collection objects. 或者,您也可以使用 RegexOptions.ExplicitCapture 選項或 n 內嵌選項,指定唯一有效的擷取是 (?<name> subexpression) 建構所指定的明確命名或編號群組。As an alternative, you can use either the RegexOptions.ExplicitCapture option or the n inline option to specify that the only valid captures are explicitly named or numbered groups that are designated by the (?<name> subexpression) construct.

下列範例顯示,當呼叫 \b\(?((\w+),?\s?)+[\.!?]\)? 方法時,如果沒有使用 Match 選項,RegexOptions.ExplicitCapture 規則運算式模式所傳回的比對相關資訊。The following example displays information about the matches returned by the \b\(?((\w+),?\s?)+[\.!?]\)? regular expression pattern when the Match method is called with and without the RegexOptions.ExplicitCapture option. 當第一個方法呼叫的輸出顯示時,規則運算式引擎會以擷取子字串的相關資訊完整填入 GroupCollectionCaptureCollection 集合物件。As the output from the first method call shows, the regular expression engine fully populates the GroupCollection and CaptureCollection collection objects with information about captured substrings. 因為呼叫第二個方法時,options 設為 RegexOptions.ExplicitCapture,所以沒有擷取群組的資訊。Because the second method is called with options set to RegexOptions.ExplicitCapture, it does not capture information on groups.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "This is the first sentence. Is it the beginning " + 
                     "of a literary masterpiece? I think not. Instead, " + 
                     "it is a nonsensical paragraph.";
      string pattern = @"\b\(?((?>\w+),?\s?)+[\.!?]\)?";
      Console.WriteLine("With implicit captures:");
      foreach (Match match in Regex.Matches(input, pattern))
      {
         Console.WriteLine("The match: {0}", match.Value);
         int groupCtr = 0;
         foreach (Group group in match.Groups)
         {
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value);
            groupCtr++;
            int captureCtr = 0;
            foreach (Capture capture in group.Captures)
            {
               Console.WriteLine("      Capture {0}: {1}", captureCtr, capture.Value);
               captureCtr++;
            }
         }
      }
      Console.WriteLine();
      Console.WriteLine("With explicit captures only:");
      foreach (Match match in Regex.Matches(input, pattern, RegexOptions.ExplicitCapture))
      {
         Console.WriteLine("The match: {0}", match.Value);
         int groupCtr = 0;
         foreach (Group group in match.Groups)
         {
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value);
            groupCtr++;
            int captureCtr = 0;
            foreach (Capture capture in group.Captures)
            {
               Console.WriteLine("      Capture {0}: {1}", captureCtr, capture.Value);
               captureCtr++;
            }
         }
      }
   }
}
// The example displays the following output:
//    With implicit captures:
//    The match: This is the first sentence.
//       Group 0: This is the first sentence.
//          Capture 0: This is the first sentence.
//       Group 1: sentence
//          Capture 0: This
//          Capture 1: is
//          Capture 2: the
//          Capture 3: first
//          Capture 4: sentence
//       Group 2: sentence
//          Capture 0: This
//          Capture 1: is
//          Capture 2: the
//          Capture 3: first
//          Capture 4: sentence
//    The match: Is it the beginning of a literary masterpiece?
//       Group 0: Is it the beginning of a literary masterpiece?
//          Capture 0: Is it the beginning of a literary masterpiece?
//       Group 1: masterpiece
//          Capture 0: Is
//          Capture 1: it
//          Capture 2: the
//          Capture 3: beginning
//          Capture 4: of
//          Capture 5: a
//          Capture 6: literary
//          Capture 7: masterpiece
//       Group 2: masterpiece
//          Capture 0: Is
//          Capture 1: it
//          Capture 2: the
//          Capture 3: beginning
//          Capture 4: of
//          Capture 5: a
//          Capture 6: literary
//          Capture 7: masterpiece
//    The match: I think not.
//       Group 0: I think not.
//          Capture 0: I think not.
//       Group 1: not
//          Capture 0: I
//          Capture 1: think
//          Capture 2: not
//       Group 2: not
//          Capture 0: I
//          Capture 1: think
//          Capture 2: not
//    The match: Instead, it is a nonsensical paragraph.
//       Group 0: Instead, it is a nonsensical paragraph.
//          Capture 0: Instead, it is a nonsensical paragraph.
//       Group 1: paragraph
//          Capture 0: Instead,
//          Capture 1: it
//          Capture 2: is
//          Capture 3: a
//          Capture 4: nonsensical
//          Capture 5: paragraph
//       Group 2: paragraph
//          Capture 0: Instead
//          Capture 1: it
//          Capture 2: is
//          Capture 3: a
//          Capture 4: nonsensical
//          Capture 5: paragraph
//    
//    With explicit captures only:
//    The match: This is the first sentence.
//       Group 0: This is the first sentence.
//          Capture 0: This is the first sentence.
//    The match: Is it the beginning of a literary masterpiece?
//       Group 0: Is it the beginning of a literary masterpiece?
//          Capture 0: Is it the beginning of a literary masterpiece?
//    The match: I think not.
//       Group 0: I think not.
//          Capture 0: I think not.
//    The match: Instead, it is a nonsensical paragraph.
//       Group 0: Instead, it is a nonsensical paragraph.
//          Capture 0: Instead, it is a nonsensical paragraph.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "This is the first sentence. Is it the beginning " + _
                            "of a literary masterpiece? I think not. Instead, " + _
                            "it is a nonsensical paragraph."
      Dim pattern As String = "\b\(?((?>\w+),?\s?)+[\.!?]\)?"
      Console.WriteLine("With implicit captures:")
      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine("The match: {0}", match.Value)
         Dim groupCtr As Integer = 0
         For Each group As Group In match.Groups
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value)
            groupCtr += 1
            Dim captureCtr As Integer = 0
            For Each capture As Capture In group.Captures
               Console.WriteLine("      Capture {0}: {1}", captureCtr, capture.Value)
               captureCtr += 1
            Next
         Next
      Next
      Console.WriteLine()
      Console.WriteLine("With explicit captures only:")
      For Each match As Match In Regex.Matches(input, pattern, RegexOptions.ExplicitCapture)
         Console.WriteLine("The match: {0}", match.Value)
         Dim groupCtr As Integer = 0
         For Each group As Group In match.Groups
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value)
            groupCtr += 1
            Dim captureCtr As Integer = 0
            For Each capture As Capture In group.Captures
               Console.WriteLine("      Capture {0}: {1}", captureCtr, capture.Value)
               captureCtr += 1
            Next
         Next
      Next
   End Sub
End Module
' The example displays the following output:
'    With implicit captures:
'    The match: This is the first sentence.
'       Group 0: This is the first sentence.
'          Capture 0: This is the first sentence.
'       Group 1: sentence
'          Capture 0: This
'          Capture 1: is
'          Capture 2: the
'          Capture 3: first
'          Capture 4: sentence
'       Group 2: sentence
'          Capture 0: This
'          Capture 1: is
'          Capture 2: the
'          Capture 3: first
'          Capture 4: sentence
'    The match: Is it the beginning of a literary masterpiece?
'       Group 0: Is it the beginning of a literary masterpiece?
'          Capture 0: Is it the beginning of a literary masterpiece?
'       Group 1: masterpiece
'          Capture 0: Is
'          Capture 1: it
'          Capture 2: the
'          Capture 3: beginning
'          Capture 4: of
'          Capture 5: a
'          Capture 6: literary
'          Capture 7: masterpiece
'       Group 2: masterpiece
'          Capture 0: Is
'          Capture 1: it
'          Capture 2: the
'          Capture 3: beginning
'          Capture 4: of
'          Capture 5: a
'          Capture 6: literary
'          Capture 7: masterpiece
'    The match: I think not.
'       Group 0: I think not.
'          Capture 0: I think not.
'       Group 1: not
'          Capture 0: I
'          Capture 1: think
'          Capture 2: not
'       Group 2: not
'          Capture 0: I
'          Capture 1: think
'          Capture 2: not
'    The match: Instead, it is a nonsensical paragraph.
'       Group 0: Instead, it is a nonsensical paragraph.
'          Capture 0: Instead, it is a nonsensical paragraph.
'       Group 1: paragraph
'          Capture 0: Instead,
'          Capture 1: it
'          Capture 2: is
'          Capture 3: a
'          Capture 4: nonsensical
'          Capture 5: paragraph
'       Group 2: paragraph
'          Capture 0: Instead
'          Capture 1: it
'          Capture 2: is
'          Capture 3: a
'          Capture 4: nonsensical
'          Capture 5: paragraph
'    
'    With explicit captures only:
'    The match: This is the first sentence.
'       Group 0: This is the first sentence.
'          Capture 0: This is the first sentence.
'    The match: Is it the beginning of a literary masterpiece?
'       Group 0: Is it the beginning of a literary masterpiece?
'          Capture 0: Is it the beginning of a literary masterpiece?
'    The match: I think not.
'       Group 0: I think not.
'          Capture 0: I think not.
'    The match: Instead, it is a nonsensical paragraph.
'       Group 0: Instead, it is a nonsensical paragraph.
'          Capture 0: Instead, it is a nonsensical paragraph.

規則運算式模式 \b\(?((?>\w+),?\s?)+[\.!?]\)? 的定義如下表所示。The regular expression pattern\b\(?((?>\w+),?\s?)+[\.!?]\)? is defined as shown in the following table.

模式Pattern 描述Description
\b 從字邊界開始。Begin at a word boundary.
\(? 比對出現零或一次的左括號 ("(")。Match zero or one occurrences of the opening parenthesis ("(").
(?>\w+),? 比對後面接著零或一個逗號的一或多個文字字元。Match one or more word characters, followed by zero or one commas. 比對文字字元時,請勿回溯。Do not backtrack when matching word characters.
\s? 比對零個或一個空白字元。Match zero or one white-space characters.
((\w+),?\s?)+ 一或多次比對一或多個文字字元、零或一個逗號及零或一個空白字元的組合。Match the combination of one or more word characters, zero or one commas, and zero or one white-space characters one or more times.
[\.!?]\)? 比對這三種標點符號中的任一種,後面接零或一個右括號 (")")。Match any of the three punctuation symbols, followed by zero or one closing parentheses (")").

您也可以使用 (?n) 內嵌元素來隱藏自動擷取。You can also use the (?n) inline element to suppress automatic captures. 下列範例會修改上一個規則運算式模式,以使用 (?n) 內嵌項目,而不是使用 RegexOptions.ExplicitCapture 選項。The following example modifies the previous regular expression pattern to use the (?n) inline element instead of the RegexOptions.ExplicitCapture option.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "This is the first sentence. Is it the beginning " + 
                     "of a literary masterpiece? I think not. Instead, " + 
                     "it is a nonsensical paragraph.";
      string pattern = @"(?n)\b\(?((?>\w+),?\s?)+[\.!?]\)?";

      foreach (Match match in Regex.Matches(input, pattern))
      {
         Console.WriteLine("The match: {0}", match.Value);
         int groupCtr = 0;
         foreach (Group group in match.Groups)
         {
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value);
            groupCtr++;
            int captureCtr = 0;
            foreach (Capture capture in group.Captures)
            {
               Console.WriteLine("      Capture {0}: {1}", captureCtr, capture.Value);
               captureCtr++;
            }
         }
      }
   }
}
// The example displays the following output:
//       The match: This is the first sentence.
//          Group 0: This is the first sentence.
//             Capture 0: This is the first sentence.
//       The match: Is it the beginning of a literary masterpiece?
//          Group 0: Is it the beginning of a literary masterpiece?
//             Capture 0: Is it the beginning of a literary masterpiece?
//       The match: I think not.
//          Group 0: I think not.
//             Capture 0: I think not.
//       The match: Instead, it is a nonsensical paragraph.
//          Group 0: Instead, it is a nonsensical paragraph.
//             Capture 0: Instead, it is a nonsensical paragraph.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "This is the first sentence. Is it the beginning " + _
                            "of a literary masterpiece? I think not. Instead, " + _
                            "it is a nonsensical paragraph."
      Dim pattern As String = "(?n)\b\(?((?>\w+),?\s?)+[\.!?]\)?"

      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine("The match: {0}", match.Value)
         Dim groupCtr As Integer = 0
         For Each group As Group In match.Groups
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value)
            groupCtr += 1
            Dim captureCtr As Integer = 0
            For Each capture As Capture In group.Captures
               Console.WriteLine("      Capture {0}: {1}", captureCtr, capture.Value)
               captureCtr += 1
            Next
         Next
      Next
   End Sub
End Module
' The example displays the following output:
'       The match: This is the first sentence.
'          Group 0: This is the first sentence.
'             Capture 0: This is the first sentence.
'       The match: Is it the beginning of a literary masterpiece?
'          Group 0: Is it the beginning of a literary masterpiece?
'             Capture 0: Is it the beginning of a literary masterpiece?
'       The match: I think not.
'          Group 0: I think not.
'             Capture 0: I think not.
'       The match: Instead, it is a nonsensical paragraph.
'          Group 0: Instead, it is a nonsensical paragraph.
'             Capture 0: Instead, it is a nonsensical paragraph.

最後,您可以使用內嵌群組項目 (?n:),針對每個群組逐一隱藏自動擷取。Finally, you can use the inline group element (?n:) to suppress automatic captures on a group-by-group basis. 下列範例會修改上一個模式,以隱藏外部群組 ((?>\w+),?\s?) 中的未具名擷取。The following example modifies the previous pattern to suppress unnamed captures in the outer group, ((?>\w+),?\s?). 請注意,這也會隱藏內部群組中的未具名擷取。Note that this suppresses unnamed captures in the inner group as well.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "This is the first sentence. Is it the beginning " + 
                     "of a literary masterpiece? I think not. Instead, " + 
                     "it is a nonsensical paragraph.";
      string pattern = @"\b\(?(?n:(?>\w+),?\s?)+[\.!?]\)?";

      foreach (Match match in Regex.Matches(input, pattern))
      {
         Console.WriteLine("The match: {0}", match.Value);
         int groupCtr = 0;
         foreach (Group group in match.Groups)
         {
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value);
            groupCtr++;
            int captureCtr = 0;
            foreach (Capture capture in group.Captures)
            {
               Console.WriteLine("      Capture {0}: {1}", captureCtr, capture.Value);
               captureCtr++;
            }
         }
      }
   }
}
// The example displays the following output:
//       The match: This is the first sentence.
//          Group 0: This is the first sentence.
//             Capture 0: This is the first sentence.
//       The match: Is it the beginning of a literary masterpiece?
//          Group 0: Is it the beginning of a literary masterpiece?
//             Capture 0: Is it the beginning of a literary masterpiece?
//       The match: I think not.
//          Group 0: I think not.
//             Capture 0: I think not.
//       The match: Instead, it is a nonsensical paragraph.
//          Group 0: Instead, it is a nonsensical paragraph.
//             Capture 0: Instead, it is a nonsensical paragraph.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "This is the first sentence. Is it the beginning " + _
                            "of a literary masterpiece? I think not. Instead, " + _
                            "it is a nonsensical paragraph."
      Dim pattern As String = "\b\(?(?n:(?>\w+),?\s?)+[\.!?]\)?"

      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine("The match: {0}", match.Value)
         Dim groupCtr As Integer = 0
         For Each group As Group In match.Groups
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value)
            groupCtr += 1
            Dim captureCtr As Integer = 0
            For Each capture As Capture In group.Captures
               Console.WriteLine("      Capture {0}: {1}", captureCtr, capture.Value)
               captureCtr += 1
            Next
         Next
      Next
   End Sub
End Module
' The example displays the following output:
'       The match: This is the first sentence.
'          Group 0: This is the first sentence.
'             Capture 0: This is the first sentence.
'       The match: Is it the beginning of a literary masterpiece?
'          Group 0: Is it the beginning of a literary masterpiece?
'             Capture 0: Is it the beginning of a literary masterpiece?
'       The match: I think not.
'          Group 0: I think not.
'             Capture 0: I think not.
'       The match: Instead, it is a nonsensical paragraph.
'          Group 0: Instead, it is a nonsensical paragraph.
'             Capture 0: Instead, it is a nonsensical paragraph.

編譯的規則運算式Compiled Regular Expressions

預設會解譯 .NET 中的規則運算式。By default, regular expressions in .NET are interpreted. Regex 物件具現化,或是呼叫靜態 Regex 方法時,會將規則運算式模式剖析成一組自訂 opcode,而解譯器會使用這些 opcode 來執行規則運算式。When a Regex object is instantiated or a static Regex method is called, the regular expression pattern is parsed into a set of custom opcodes, and an interpreter uses these opcodes to run the regular expression. 這需要有所取捨:要將初始化規則運算式引擎的成本降到最低,就會犧牲執行時期效能。This involves a tradeoff: The cost of initializing the regular expression engine is minimized at the expense of run-time performance.

您可以使用 RegexOptions.Compiled 選項,以編譯的規則運算式來取代解譯的規則運算式。You can use compiled instead of interpreted regular expressions by using the RegexOptions.Compiled option. 在這個情況下,將模式傳遞至規則運算式時,會將該模式剖析成一組自訂作業碼,然後再轉換成 Microsoft 中繼語言 (MSIL),可直接傳遞至通用語言執行平台。In this case, when a pattern is passed to the regular expression engine, it is parsed into a set of opcodes and then converted to Microsoft intermediate language (MSIL), which can be passed directly to the common language runtime. 編譯的規則運算式可充分提升執行時期效能,但會犧牲初始化時間。Compiled regular expressions maximize run-time performance at the expense of initialization time.

注意

若要編譯規則運算式,唯一的方法就是提供 RegexOptions.Compiled 值給 options 類別建構函式或靜態模式比對方法的 Regex 參數。A regular expression can be compiled only by supplying the RegexOptions.Compiled value to the options parameter of a Regex class constructor or a static pattern-matching method. 無法以內嵌選項來提供此值。It is not available as an inline option.

在呼叫靜態和執行個體規則運算式時,都可以使用編譯的規則運算式。You can use compiled regular expressions in calls to both static and instance regular expressions. 在靜態規則運算式中,會將 RegexOptions.Compiled 選項傳遞至規則運算式模式比對方法的 options 參數。In static regular expressions, the RegexOptions.Compiled option is passed to the options parameter of the regular expression pattern-matching method. 在執行個體規則運算式中,則會傳遞至 options 類別建構函式的 Regex 參數。In instance regular expressions, it is passed to the options parameter of the Regex class constructor. 在這兩個情況中,都會增強效能。In both cases, it results in enhanced performance.

不過,只有在下列條件下,效能才會提升:However, this improvement in performance occurs only under the following conditions:

  • 在對規則運算式模式比對方法的多個呼叫中,都會使用代表特定規則運算式的 Regex 物件。A Regex object that represents a particular regular expression is used in multiple calls to regular expression pattern-matching methods.

  • Regex 物件不能超出範圍,因此可以重複使用。The Regex object is not allowed to go out of scope, so it can be reused.

  • 在對規則運算式模式比對方法的多個呼叫中,會使用靜態規則運算式。A static regular expression is used in multiple calls to regular expression pattern-matching methods. (效能提升是有可能的,因為規則運算式引擎會快取靜態方法呼叫中所使用的規則運算式。)(The performance improvement is possible because regular expressions used in static method calls are cached by the regular expression engine.)

注意

RegexOptions.Compiled 選項與 Regex.CompileToAssembly 方法無關,此方法會建立特殊用途的組件,其中包含預先定義的編譯規則運算式。The RegexOptions.Compiled option is unrelated to the Regex.CompileToAssembly method, which creates a special-purpose assembly that contains predefined compiled regular expressions.

忽略空白字元Ignore White Space

依預設,規則運算式模式中的空白字元很重要;它會強制規則運算式引擎比對輸入字串中的空白字元。By default, white space in a regular expression pattern is significant; it forces the regular expression engine to match a white-space character in the input string. 因此,規則運算式 "\b\w+\s" 和 "\b\w+" 是大致相等的規則運算式。Because of this, the regular expression "\b\w+\s" and "\b\w+ " are roughly equivalent regular expressions. 此外,在規則運算式模式中遇到數字符號 (#) 時,會將其解譯成常值字元,以供比對。In addition, when the number sign (#) is encountered in a regular expression pattern, it is interpreted as a literal character to be matched.

RegexOptions.IgnorePatternWhitespace 選項 (或 x 內嵌選項) 會變更此預設行為,如下所示:The RegexOptions.IgnorePatternWhitespace option, or the x inline option, changes this default behavior as follows:

  • 規則運算式模式中未逸出的空白字元會被忽略。Unescaped white space in the regular expression pattern is ignored. 若要在規則運算式模式中使用空白字元,就必須將它逸出 (例如,\s 或 "\")。To be part of a regular expression pattern, white-space characters must be escaped (for example, as \s or "\ ").

  • 數字符號 (#) 會解譯成註解的開頭,而不是常值字元。The number sign (#) is interpreted as the beginning of a comment, rather than as a literal character. 規則運算式模式中,從 # 字元到字串結尾的所有文字會被解譯成註解。All text in the regular expression pattern from the # character to the end of the string is interpreted as a comment.

但在下列案例中,即使您使用 RegexOptions.IgnorePatternWhitespace 選項,也不會忽略規則運算式中的空白字元:However, in the following cases, white-space characters in a regular expression aren't ignored, even if you use the RegexOptions.IgnorePatternWhitespace option:

  • 字元類別中的空白字元一律解譯為常值。White space within a character class is always interpreted literally. 例如,規則運算式模式 [ .,;:] 會比對任何單一空白字元、句點、逗號、分號或冒號。For example, the regular expression pattern [ .,;:] matches any single white-space character, period, comma, semicolon, or colon.

  • 方括號數量詞中不允許空白字元,例如 {n}{n,}{n,m}White space isn't allowed within a bracketed quantifier, such as {n}, {n,}, and {n,m}. 例如,規則運算式模式 \d{1, 3} 無法比對從一到三位數的任何數字序列,因為其中包含空白字元。For example, the regular expression pattern \d{1, 3} fails to match any sequences of digits from one to three digits because it contains a white-space character.

  • 引進語言項目的字元序列中,不允許空白字元。White space isn't allowed within a character sequence that introduces a language element. 例如:For example:

    • 語言元素 (?:subexpression) 代表非擷取群組,而該項目的 (?: 部分不能有內嵌空格。The language element (?:subexpression) represents a noncapturing group, and the (?: portion of the element can't have embedded spaces. 模式 (? :subexpression) 會在執行時期擲回 ArgumentException,因為規則運算式引擎無法剖析該模式,而且模式 ( ?:subexpression) 無法比對 subexpressionThe pattern (? :subexpression) throws an ArgumentException at run time because the regular expression engine can't parse the pattern, and the pattern ( ?:subexpression) fails to match subexpression.

    • 語言元素 \p{name} 代表 Unicode 類別或具名資料區塊,不能在此元素的 \p{ 部分中包含內嵌空格。The language element \p{name}, which represents a Unicode category or named block, can't include embedded spaces in the \p{ portion of the element. 如果包含空白字元,則此項目會在執行時期擲回 ArgumentExceptionIf you do include a white space, the element throws an ArgumentException at run time.

啟用此選項有助於簡化通常很難剖析及了解的規則運算式。Enabling this option helps simplify regular expressions that are often difficult to parse and to understand. 其增進了可讀性,並且讓規則運算式可以被記載下來。It improves readability, and makes it possible to document a regular expression.

下列範例定義下列規則運算式模式:The following example defines the following regular expression pattern:

\b \(? ( (?>\w+) ,?\s? )+ [\.!?] \)? # Matches an entire sentence.

此模式類似僅明確擷取一節中定義的模式,只是其會使用 RegexOptions.IgnorePatternWhitespace 選項來忽略模式空白字元。This pattern is similar to the pattern defined in the Explicit Captures Only section, except that it uses the RegexOptions.IgnorePatternWhitespace option to ignore pattern white space.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "This is the first sentence. Is it the beginning " + 
                     "of a literary masterpiece? I think not. Instead, " + 
                     "it is a nonsensical paragraph.";
      string pattern = @"\b \(? ( (?>\w+) ,?\s? )+ [\.!?] \)? # Matches an entire sentence.";

      foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnorePatternWhitespace))
         Console.WriteLine(match.Value);
   }
}
// The example displays the following output:
//       This is the first sentence.
//       Is it the beginning of a literary masterpiece?
//       I think not.
//       Instead, it is a nonsensical paragraph.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "This is the first sentence. Is it the beginning " + _
                            "of a literary masterpiece? I think not. Instead, " + _
                            "it is a nonsensical paragraph."
      Dim pattern As String = "\b \(? ( (?>\w+) ,?\s? )+  [\.!?] \)? # Matches an entire sentence."
      
      For Each match As Match In Regex.Matches(input, pattern, RegexOptions.IgnorePatternWhitespace)
         Console.WriteLine(match.Value)
      Next
   End Sub
End Module
' The example displays the following output:
'       This is the first sentence.
'       Is it the beginning of a literary masterpiece?
'       I think not.
'       Instead, it is a nonsensical paragraph.

下列範例使用內嵌選項 (?x) 來忽略模式空白字元。The following example uses the inline option (?x) to ignore pattern white space.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "This is the first sentence. Is it the beginning " + 
                     "of a literary masterpiece? I think not. Instead, " + 
                     "it is a nonsensical paragraph.";
      string pattern = @"(?x)\b \(? ( (?>\w+) ,?\s? )+  [\.!?] \)? # Matches an entire sentence.";

      foreach (Match match in Regex.Matches(input, pattern))
         Console.WriteLine(match.Value);
   }
}
// The example displays the following output:
//       This is the first sentence.
//       Is it the beginning of a literary masterpiece?
//       I think not.
//       Instead, it is a nonsensical paragraph.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "This is the first sentence. Is it the beginning " + _
                            "of a literary masterpiece? I think not. Instead, " + _
                            "it is a nonsensical paragraph."
      Dim pattern As String = "(?x)\b \(? ( (?>\w+) ,?\s? )+  [\.!?] \)? # Matches an entire sentence."
      
      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine(match.Value)
      Next
   End Sub
End Module
' The example displays the following output:
'       This is the first sentence.
'       Is it the beginning of a literary masterpiece?
'       I think not.
'       Instead, it is a nonsensical paragraph.

由右至左模式Right-to-Left Mode

依預設,規則運算式引擎會由左至右搜尋。By default, the regular expression engine searches from left to right. 您可以使用 RegexOptions.RightToLeft 選項來反轉搜尋方向。You can reverse the search direction by using the RegexOptions.RightToLeft option. 此搜尋會自動從字串最後一個字元的位置開始。The search automatically begins at the last character position of the string. 針對包含開始位置參數的模式比對方法,例如 Regex.Match(String, Int32),開始位置是要開始搜尋之最右邊字元位置的索引。For pattern-matching methods that include a starting position parameter, such as Regex.Match(String, Int32), the starting position is the index of the rightmost character position at which the search is to begin.

注意

若要使用由右至左模式,唯一的方法就是提供 RegexOptions.RightToLeft 值給 options 類別建構函式或靜態模式比對方法的 Regex 參數。Right-to-left pattern mode is available only by supplying the RegexOptions.RightToLeft value to the options parameter of a Regex class constructor or static pattern-matching method. 無法以內嵌選項來提供此值。It is not available as an inline option.

RegexOptions.RightToLeft 選項只會變更搜尋方向,並不會由右至左解譯規則運算式模式。The RegexOptions.RightToLeft option changes the search direction only; it does not interpret the regular expression pattern from right to left. 例如,規則運算式 \bb\w+\s 會比對字母 "b" 開頭、後接空白字元的文字。For example, the regular expression \bb\w+\s matches words that begin with the letter "b" and are followed by a white-space character. 在下列範例中,輸入字串是由包含一或數個 "b" 字元的三個單字所組成。In the following example, the input string consists of three words that include one or more "b" characters. 第一個單字以 "b" 開頭,第二個單字以 "b" 結尾,而第三個單字中間包含兩個 "b" 字元。The first word begins with "b", the second ends with "b", and the third includes two "b" characters in the middle of the word. 如範例輸出所示,只有第一個單字符合規則運算式模式。As the output from the example shows, only the first word matches the regular expression pattern.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\bb\w+\s";
      string input = "builder rob rabble";
      foreach (Match match in Regex.Matches(input, pattern, RegexOptions.RightToLeft))
         Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);     
   }
}
// The example displays the following output:
//       'builder ' found at position 0.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\bb\w+\s"
      Dim input As String = "builder rob rabble"
      For Each match As Match In Regex.Matches(input, pattern, RegexOptions.RightToLeft)
         Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)     
      Next
   End Sub
End Module
' The example displays the following output:
'       'builder ' found at position 0.

另請注意,右合樣判斷提示 ((?=subexpression) 語言元素) 和左合樣判斷提示 ((?<=subexpression) 語言元素) 沒有變更方向。Also note that the lookahead assertion (the (?=subexpression) language element) and the lookbehind assertion (the (?<=subexpression) language element) do not change direction. 右合樣判斷提示朝右看,而左合樣判斷提示朝左看。The lookahead assertions look to the right; the lookbehind assertions look to the left. 例如,規則運算式 (?<=\d{1,2}\s)\w+,?\s\d{4} 使用左合樣判斷提示來測試月份名稱前面的日期。For example, the regular expression (?<=\d{1,2}\s)\w+,?\s\d{4} uses the lookbehind assertion to test for a date that precedes a month name. 然後規則運算式會比對月和年。The regular expression then matches the month and the year. 如需右合樣判斷提示和左合樣判斷提示的詳細資訊,請參閱群組建構For information on lookahead and lookbehind assertions, see Grouping Constructs.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string[] inputs = { "1 May 1917", "June 16, 2003" };
      string pattern = @"(?<=\d{1,2}\s)\w+,?\s\d{4}";
      
      foreach (string input in inputs)
      {
         Match match = Regex.Match(input, pattern, RegexOptions.RightToLeft);
         if (match.Success)
            Console.WriteLine("The date occurs in {0}.", match.Value);
         else
            Console.WriteLine("{0} does not match.", input);
      }
   }
}
// The example displays the following output:
//       The date occurs in May 1917.
//       June 16, 2003 does not match.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim inputs() As String = { "1 May 1917", "June 16, 2003" }
      Dim pattern As String = "(?<=\d{1,2}\s)\w+,?\s\d{4}"
      
      For Each input As String In inputs
         Dim match As Match = Regex.Match(input, pattern, RegexOptions.RightToLeft)
         If match.Success Then
            Console.WriteLine("The date occurs in {0}.", match.Value)
         Else
            Console.WriteLine("{0} does not match.", input)
         End If
      Next
   End Sub
End Module
' The example displays the following output:
'       The date occurs in May 1917.
'       June 16, 2003 does not match.

規則運算式模式的定義如下表所示。The regular expression pattern is defined as shown in the following table.

模式Pattern 描述Description
(?<=\d{1,2}\s) 相符項目的開頭前面必須要有一個或兩個十進位數字後接空格。The beginning of the match must be preceded by one or two decimal digits followed by a space.
\w+ 比對一個或多個文字字元。Match one or more word characters.
,? 比對零或一個逗號字元。Match zero or one comma characters.
\s 比對空白字元。Match a white-space character.
\d{4} 比對四個十進位數字。Match four decimal digits.

ECMAScript 相符行為ECMAScript Matching Behavior

依預設,在比對規則運算式模式與輸入文字時,規則運算式引擎會使用標準行為。By default, the regular expression engine uses canonical behavior when matching a regular expression pattern to input text. 不過,您可以指定 RegexOptions.ECMAScript 選項,以指示規則運算式引擎使用 ECMAScript 相符行為。However, you can instruct the regular expression engine to use ECMAScript matching behavior by specifying the RegexOptions.ECMAScript option.

注意

若要使用符合 ECMAScript 的行為,唯一的方法就是提供 RegexOptions.ECMAScript 值給 options 類別建構函式或靜態模式比對方法的 Regex 參數。ECMAScript-compliant behavior is available only by supplying the RegexOptions.ECMAScript value to the options parameter of a Regex class constructor or static pattern-matching method. 無法以內嵌選項來提供此值。It is not available as an inline option.

RegexOptions.ECMAScript 選項只能與 RegexOptions.IgnoreCaseRegexOptions.Multiline 選項合併使用。The RegexOptions.ECMAScript option can be combined only with the RegexOptions.IgnoreCase and RegexOptions.Multiline options. 在規則運算式中使用任何其他選項將會導致 ArgumentOutOfRangeExceptionThe use of any other option in a regular expression results in an ArgumentOutOfRangeException.

ECMAScript 的行為與標準規則運算式有三個不同層面:字元類別語法、自我參考擷取群組,以及八進位與反向參考解譯。The behavior of ECMAScript and canonical regular expressions differs in three areas: character class syntax, self-referencing capturing groups, and octal versus backreference interpretation.

  • 字元類別語法。Character class syntax. 因為標準規則運算式支援 Unicode,而 ECMAScript 不支援,所以 ECMAScript 中的字元類別有較多的語法限制,而且有些字元類別語言項目有不同的意義。Because canonical regular expressions support Unicode whereas ECMAScript does not, character classes in ECMAScript have a more limited syntax, and some character class language elements have a different meaning. 例如,ECMAScript 不支援語言項目 (例如 Unicode 類別) 或資料區塊項目 \p\PFor example, ECMAScript does not support language elements such as the Unicode category or block elements \p and \P. 同樣地,使用 ECMAScript 時,\w 項目 (用來比對文字字元) 同等於 [a-zA-Z_0-9] 字元類別,使用標準行為時,同等於 [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}\p{Lm}]Similarly, the \w element, which matches a word character, is equivalent to the [a-zA-Z_0-9] character class when using ECMAScript and [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}\p{Lm}] when using canonical behavior. 如需詳細資訊,請參閱字元類別For more information, see Character Classes.

    下列範例說明標準與 ECMAScript 模式比對之間的差異。The following example illustrates the difference between canonical and ECMAScript pattern matching. 其定義規則運算式 \b(\w+\s*)+,可比對後接空白字元的文字。It defines a regular expression, \b(\w+\s*)+, that matches words followed by white-space characters. 該輸入包含兩個字串,一個使用 Latin 字元集,另一個使用 Cyrillic 字元集。The input consists of two strings, one that uses the Latin character set and the other that uses the Cyrillic character set. 如輸出所示,呼叫使用 ECMAScript 比對的 Regex.IsMatch(String, String, RegexOptions) 方法時,無法比對 Cyrillic 文字,而使用標準比對的方法呼叫則可比對這些文字。As the output shows, the call to the Regex.IsMatch(String, String, RegexOptions) method that uses ECMAScript matching fails to match the Cyrillic words, whereas the method call that uses canonical matching does match these words.

    using System;
    using System.Text.RegularExpressions;
    
    public class Example
    {
       public static void Main()
       {
          string[] values = { "целый мир", "the whole world" };
          string pattern = @"\b(\w+\s*)+";
          foreach (var value in values)
          {
             Console.Write("Canonical matching: ");
             if (Regex.IsMatch(value, pattern))
                Console.WriteLine("'{0}' matches the pattern.", value);
             else
                Console.WriteLine("{0} does not match the pattern.", value);
             
             Console.Write("ECMAScript matching: ");
             if (Regex.IsMatch(value, pattern, RegexOptions.ECMAScript))
                Console.WriteLine("'{0}' matches the pattern.", value);
             else
                Console.WriteLine("{0} does not match the pattern.", value);
             Console.WriteLine();
          }
       }
    }
    // The example displays the following output:
    //       Canonical matching: 'целый мир' matches the pattern.
    //       ECMAScript matching: целый мир does not match the pattern.
    //       
    //       Canonical matching: 'the whole world' matches the pattern.
    //       ECMAScript matching: 'the whole world' matches the pattern.
    
    Imports System.Text.RegularExpressions
    
    Module Example
       Public Sub Main()
          Dim values() As String = { "целый мир", "the whole world" }
          Dim pattern As String = "\b(\w+\s*)+"
          For Each value In values
             Console.Write("Canonical matching: ")
             If Regex.IsMatch(value, pattern)
                Console.WriteLine("'{0}' matches the pattern.", value)
             Else
                Console.WriteLine("{0} does not match the pattern.", value)
             End If
             
             Console.Write("ECMAScript matching: ")
             If Regex.IsMatch(value, pattern, RegexOptions.ECMAScript)
                Console.WriteLine("'{0}' matches the pattern.", value)
             Else
                Console.WriteLine("{0} does not match the pattern.", value)
             End If
             Console.WriteLine()
          Next
       End Sub
    End Module
    ' The example displays the following output:
    '       Canonical matching: 'целый мир' matches the pattern.
    '       ECMAScript matching: целый мир does not match the pattern.
    '       
    '       Canonical matching: 'the whole world' matches the pattern.
    '       ECMAScript matching: 'the whole world' matches the pattern.
    
  • 自我參考擷取群組。Self-referencing capturing groups. 具有自我反向參考的規則運算式擷取類別必須以每個擷取反覆項目來更新。A regular expression capture class with a backreference to itself must be updated with each capture iteration. 如下列範例所示,使用 ECMAScript 時,此功能可讓規則運算式 ((a+)(\1) ?)+ 比對輸入字串 " aa aaaa aaaaaa ",使用標準比對時則不能。As the following example shows, this feature enables the regular expression ((a+)(\1) ?)+ to match the input string " aa aaaa aaaaaa " when using ECMAScript, but not when using canonical matching.

    using System;
    using System.Text.RegularExpressions;
    
    public class Example
    {
       static string pattern;
       
       public static void Main()
       {
          string input = "aa aaaa aaaaaa "; 
          pattern = @"((a+)(\1) ?)+";
          
          // Match input using canonical matching.
          AnalyzeMatch(Regex.Match(input, pattern));
          
          // Match input using ECMAScript.
          AnalyzeMatch(Regex.Match(input, pattern, RegexOptions.ECMAScript));
       }   
       
       private static void AnalyzeMatch(Match m)
       {
          if (m.Success)
          {
             Console.WriteLine("'{0}' matches {1} at position {2}.",  
                               pattern, m.Value, m.Index);
             int grpCtr = 0;
             foreach (Group grp in m.Groups)
             {
                Console.WriteLine("   {0}: '{1}'", grpCtr, grp.Value);
                grpCtr++;
                int capCtr = 0;
                foreach (Capture cap in grp.Captures)
                {
                   Console.WriteLine("      {0}: '{1}'", capCtr, cap.Value);
                   capCtr++;
                }
             }
          }
          else
          {
             Console.WriteLine("No match found.");
          }   
          Console.WriteLine();
       }
    }
    // The example displays the following output:
    //    No match found.
    //    
    //    '((a+)(\1) ?)+' matches aa aaaa aaaaaa  at position 0.
    //       0: 'aa aaaa aaaaaa '
    //          0: 'aa aaaa aaaaaa '
    //       1: 'aaaaaa '
    //          0: 'aa '
    //          1: 'aaaa '
    //          2: 'aaaaaa '
    //       2: 'aa'
    //          0: 'aa'
    //          1: 'aa'
    //          2: 'aa'
    //       3: 'aaaa '
    //          0: ''
    //          1: 'aa '
    //          2: 'aaaa '
    
    Imports System.Text.RegularExpressions
    
    Module Example
       Dim pattern As String
       
       Public Sub Main()
          Dim input As String = "aa aaaa aaaaaa " 
          pattern = "((a+)(\1) ?)+"
          
          ' Match input using canonical matching.
          AnalyzeMatch(Regex.Match(input, pattern))
          
          ' Match input using ECMAScript.
          AnalyzeMatch(Regex.Match(input, pattern, RegexOptions.ECMAScript))
       End Sub   
       
       Private Sub AnalyzeMatch(m As Match)
          If m.Success
             Console.WriteLine("'{0}' matches {1} at position {2}.", _ 
                               pattern, m.Value, m.Index)
             Dim grpCtr As Integer = 0
             For Each grp As Group In m.Groups
                Console.WriteLine("   {0}: '{1}'", grpCtr, grp.Value)
                grpCtr += 1
                Dim capCtr As Integer = 0
                For Each cap As Capture In grp.Captures
                   Console.WriteLine("      {0}: '{1}'", capCtr, cap.Value)
                   capCtr += 1
                Next
             Next
          Else
             Console.WriteLine("No match found.")
          End If   
          Console.WriteLine()
       End Sub
    End Module
    ' The example displays the following output:
    '    No match found.
    '    
    '    '((a+)(\1) ?)+' matches aa aaaa aaaaaa  at position 0.
    '       0: 'aa aaaa aaaaaa '
    '          0: 'aa aaaa aaaaaa '
    '       1: 'aaaaaa '
    '          0: 'aa '
    '          1: 'aaaa '
    '          2: 'aaaaaa '
    '       2: 'aa'
    '          0: 'aa'
    '          1: 'aa'
    '          2: 'aa'
    '       3: 'aaaa '
    '          0: ''
    '          1: 'aa '
    '          2: 'aaaa '
    

    規則運算式的定義如下表所示。The regular expression is defined as shown in the following table.

    模式Pattern 描述Description
    (a+)(a+) 比對字母 "a" 一次或多次。Match the letter "a" one or more times. 這是第二個擷取群組。This is the second capturing group.
    (\1)(\1) 比對第一個擷取群組所擷取的子字串。Match the substring captured by the first capturing group. 這是第三個擷取群組。This is the third capturing group.
    ?? 比對零或一個空白字元。Match zero or one space characters.
    ((a+)(\1) ?)+((a+)(\1) ?)+ 一次或多次比對一個或多個 "a" 字元,後面接符合第一個擷取群組的字串,後面再接零或一個空白字元。Match the pattern of one or more "a" characters followed by a string that matches the first capturing group followed by zero or one space characters one or more times. 這是第一個擷取群組。This is the first capturing group.
  • 八進位逸出與反向參考之間模棱兩可的解決方案。Resolution of ambiguities between octal escapes and backreferences. 下表總結以標準和 ECMAScript 規則運算式來進行八進位與反向參考解譯的差異。The following table summarizes the differences in octal versus backreference interpretation by canonical and ECMAScript regular expressions.

    規則運算式Regular expression 標準行為Canonical behavior ECMAScript 行為ECMAScript behavior
    \0 後接 0 到 2 個八進位數字\0 followed by 0 to 2 octal digits 解譯成八進位。Interpret as an octal. 例如,\044 一律解譯成八進位值,且意思是 "$"。For example, \044 is always interpreted as an octal value and means "$". 相同行為。Same behavior.
    \ 後接 1 到 9 的數字,後面不再接其他十進位數字。\ followed by a digit from 1 to 9, followed by no additional decimal digits, 解譯成反向參考。Interpret as a backreference. 例如,\9 一律表示反向參考 9,即使第 9 個擷取群組不存在也一樣。For example, \9 always means backreference 9, even if a ninth capturing group does not exist. 如果擷取群組不存在,規則運算式剖析器會擲回 ArgumentExceptionIf the capturing group does not exist, the regular expression parser throws an ArgumentException. 如果單一十進位數字擷取群組存在,則反向參考至該數字。If a single decimal digit capturing group exists, backreference to that digit. 否則,會將該值解譯成常值。Otherwise, interpret the value as a literal.
    \ 後接 1 到 9 的數字,後面再接其他十進位數字。\ followed by a digit from 1 to 9, followed by additional decimal digits 將這些數字解譯成十進位值。Interpret the digits as a decimal value. 如果該擷取群組存在,則將運算式解譯成反向參考。If that capturing group exists, interpret the expression as a backreference.

    否則,解譯前置八進位數字至八進位 377;也就是說,僅考慮該值的低 8 位元。Otherwise, interpret the leading octal digits up to octal 377; that is, consider only the low 8 bits of the value. 將其餘數字解譯成常值。Interpret the remaining digits as literals. 例如,在運算式 \3000 中,如果擷取群組 300 存在,則解譯成反向參考 300;如果擷取群組 300 不存在,則解譯成八進位 300 後接 0。For example, in the expression \3000, if capturing group 300 exists, interpret as backreference 300; if capturing group 300 does not exist, interpret as octal 300 followed by 0.
    盡可能將多位數字轉換成可以參考擷取的十進位值,以解譯成反向參考。Interpret as a backreference by converting as many digits as possible to a decimal value that can refer to a capture. 如果沒有數字可供轉換,則使用前置八進位數字至八進位 377,以解譯成八進位;將其餘數字解譯成常值。If no digits can be converted, interpret as an octal by using the leading octal digits up to octal 377; interpret the remaining digits as literals.

使用不因國別而異的文化特性比較Comparison Using the Invariant Culture

依預設,當規則運算式引擎執行不區分大小寫比較時,會使用目前文化特性的大小寫慣例來判定相等的大小寫字元。By default, when the regular expression engine performs case-insensitive comparisons, it uses the casing conventions of the current culture to determine equivalent uppercase and lowercase characters.

不過,某些類型的比較並不希望有此行為,尤其是將使用者輸入與系統資源名稱 (例如密碼、檔案或 URL) 做比較時。However, this behavior is undesirable for some types of comparisons, particularly when comparing user input to the names of system resources, such as passwords, files, or URLs. 下列範例說明這種案例。The following example illustrates such as scenario. 程式碼的目的是要封鎖存取前面加上 FILE:// 之 URL 的任何資源。The code is intended to block access to any resource whose URL is prefaced with FILE://. 規則運算式使用規則運算式 $FILE://,嘗試不區分大小寫來比對字串。The regular expression attempts a case-insensitive match with the string by using the regular expression $FILE://. 不過,當目前系統文化特性為 tr-TR (Turkish-Turkey) 時,"I" 並不是 "i" 的相等大寫字元。However, when the current system culture is tr-TR (Turkish-Turkey), "I" is not the uppercase equivalent of "i". 因此,呼叫 Regex.IsMatch 方法時,會傳回 false,並允許存取該檔案。As a result, the call to the Regex.IsMatch method returns false, and access to the file is allowed.

CultureInfo defaultCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");

string input = "file://c:/Documents.MyReport.doc";
string pattern = "FILE://";

Console.WriteLine("Culture-sensitive matching ({0} culture)...", 
                  Thread.CurrentThread.CurrentCulture.Name);
if (Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase))
   Console.WriteLine("URLs that access files are not allowed.");      
else
   Console.WriteLine("Access to {0} is allowed.", input);

Thread.CurrentThread.CurrentCulture = defaultCulture;
// The example displays the following output:
//       Culture-sensitive matching (tr-TR culture)...
//       Access to file://c:/Documents.MyReport.doc is allowed.
Dim defaultCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
Thread.CurrentThread.CurrentCulture = New CultureInfo("tr-TR")

Dim input As String = "file://c:/Documents.MyReport.doc"
Dim pattern As String = "$FILE://"

Console.WriteLine("Culture-sensitive matching ({0} culture)...", _
                  Thread.CurrentThread.CurrentCulture.Name)
If Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase) Then
   Console.WriteLine("URLs that access files are not allowed.")      
Else
   Console.WriteLine("Access to {0} is allowed.", input)
End If

Thread.CurrentThread.CurrentCulture = defaultCulture
' The example displays the following output:
'       Culture-sensitive matching (tr-TR culture)...
'       Access to file://c:/Documents.MyReport.doc is allowed.

注意

如需區分大小寫和使用不因國別而異的文化特性之字串比較的詳細資訊,請參閱在 .NET Framework 中使用字串的最佳作法For more information about string comparisons that are case-sensitive and that use the invariant culture, see Best Practices for Using Strings.

您可以不要使用不因國別而異的文化特性的不區分大小寫比較,而是指定 RegexOptions.CultureInvariant 選項來忽略語言中的文化特性差異,並使用不因國別而異的文化特性的慣例。Instead of using the case-insensitive comparisons of the current culture, you can specify the RegexOptions.CultureInvariant option to ignore cultural differences in language and to use the conventions of the invariant culture.

注意

若要使用不因國別而異的文化特性的比較,唯一的方法就是提供 RegexOptions.CultureInvariant 值給 options 類別建構函式或靜態模式比對方法的 Regex 參數。Comparison using the invariant culture is available only by supplying the RegexOptions.CultureInvariant value to the options parameter of a Regex class constructor or static pattern-matching method. 無法以內嵌選項來提供此值。It is not available as an inline option.

下列範例與上一個範例相同,差別在於呼叫靜態 Regex.IsMatch(String, String, RegexOptions) 方法時,是使用包含 RegexOptions.CultureInvariant 的選項。The following example is identical to the previous example, except that the static Regex.IsMatch(String, String, RegexOptions) method is called with options that include RegexOptions.CultureInvariant. 即使目前的文化特性設為 Turkish (Turkey),規則運算式引擎還是可以成功比對 "FILE" 和 "file",並封鎖存取檔案資源。Even when the current culture is set to Turkish (Turkey), the regular expression engine is able to successfully match "FILE" and "file" and block access to the file resource.

CultureInfo defaultCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");

string input = "file://c:/Documents.MyReport.doc";
string pattern = "FILE://";

Console.WriteLine("Culture-insensitive matching...");
if (Regex.IsMatch(input, pattern, 
                  RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)) 
   Console.WriteLine("URLs that access files are not allowed.");
else
   Console.WriteLine("Access to {0} is allowed.", input);

Thread.CurrentThread.CurrentCulture = defaultCulture;
// The example displays the following output:
//       Culture-insensitive matching...
//       URLs that access files are not allowed.
Dim defaultCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
Thread.CurrentThread.CurrentCulture = New CultureInfo("tr-TR")

Dim input As String = "file://c:/Documents.MyReport.doc"
Dim pattern As String = "$FILE://"

Console.WriteLine("Culture-insensitive matching...")
If Regex.IsMatch(input, pattern, _
               RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant) Then
   Console.WriteLine("URLs that access files are not allowed.")      
Else
   Console.WriteLine("Access to {0} is allowed.", input)
End If
Thread.CurrentThread.CurrentCulture = defaultCulture
' The example displays the following output:
'        Culture-insensitive matching...
'        URLs that access files are not allowed.

請參閱See also