RegexOptions 列舉

定義

提供用來設定規則運算式 (Regular Expression) 選項的列舉值。

此列舉支援其成員值的位元組合。

public enum class RegexOptions
[System.Flags]
public enum RegexOptions
[<System.Flags>]
type RegexOptions = 
Public Enum RegexOptions
繼承
RegexOptions
屬性

欄位

Compiled 8

指定正則運算式會編譯為 MSIL 程式碼,而不是解譯。 編譯的規則運算式可充分提升執行時期效能,但會犧牲初始化時間。 在呼叫 Options 方法時,不應將這個值指派至 CompileToAssembly(RegexCompilationInfo[], AssemblyName) 屬性。 如需詳細資訊,請參閱 正則運算式選項 一文中的一節。

CultureInvariant 512

指定忽略語言中的文化差異。 如需詳細資訊,請參閱正則 運算式選項 一文中的一節。

ECMAScript 256

啟用運算式的 ECMAScript 相容行為。 這個值只能結合 IgnoreCaseMultilineCompiled 值使用, 將這個值與任何其他值一起使用都將導致例外狀況。

For more information on the ECMAScript option, see the "ECMAScript Matching Behavior" section in the Regular Expression Options article.

ExplicitCapture 4

指定表單的唯一有效擷取是明確命名或編號的群組 (? <name > ...) 。這可讓未命名的括弧做為非擷取群組,而不使用運算式的語法 (?:...) 。如需詳細資訊,請參閱 正則運算式選項 一文中的一節。

IgnoreCase 1

指定區分大小寫的比對。 如需詳細資訊,請參閱 正則運算式選項 一文中的「不區分大小寫比對」一節。

IgnorePatternWhitespace 32

從模式排除未逸出的空白字元 (White Space),並啟用以 # 標記的註解。 不過,這個值不會影響或排除字元類別中的空白字元、數值數量詞,或是標示個別規則運算式語言項目開始的權杖。 如需詳細資訊,請參閱規則運算式選項一文中的<忽略空白字元>一節。

Multiline 2

多行模式。 變更 ^ 和 $ 的意義以致它們分別在任何一行的開頭和結尾做比對,而不只是整個字串的開頭和結尾。 如需詳細資訊,請參閱 正則運算式選項 一文中的一節。

NonBacktracking 1024

使用可避免回溯的方法進行比對,並保證輸入長度的線性時間處理。

None 0

指定無選項設定。 如需正則運算式引擎之預設行為的詳細資訊,請參閱 正則運算式選項 一文中的一節。

RightToLeft 64

指定搜尋將由右至左,而非由左至右。 如需詳細資訊,請參閱 正則運算式選項 一文中的一節。

Singleline 16

指定單行模式。 變更點 (.) 的意義,使它符合一切字元 (而不是 \n 之外的一切字元)。 如需詳細資訊,請參閱 正則運算式選項 一文中的一節。

範例

下列範例會定義兩個正則運算式,這些正則運算式會識別文字中的重複單字,但會使用不同的 RegexOptions 值具現化。 第一個正則運算式不區分大小寫;判斷單字是否與上述單字相同時,會忽略 case。 第二個正則運算式區分大小寫;單字必須完全符合上述單字的大小寫,才能視為重複。

using System;
using System.Text.RegularExpressions;

public class Test
{
    public static void Main ()
    {
        // Define a case-insensitive regular expression for repeated words.
        Regex rxInsensitive = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
          RegexOptions.Compiled | RegexOptions.IgnoreCase);
        // Define a case-sensitive regular expression for repeated words.
        Regex rxSensitive = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
          RegexOptions.Compiled);

        // Define a test string.        
        string text = "The the quick brown fox  fox jumps over the lazy dog dog.";
        
        // Find matches using case-insensitive regular expression.
        MatchCollection matches = rxInsensitive.Matches(text);

        // Report the number of matches found.
        Console.WriteLine("{0} matches found in:\n   {1}", 
                          matches.Count, 
                          text);

        // Report on each match.
        foreach (Match match in matches)
        {
            GroupCollection groups = match.Groups;
            Console.WriteLine("'{0}' repeated at positions {1} and {2}",  
                              groups["word"].Value, 
                              groups[0].Index, 
                              groups[1].Index);
        }
        Console.WriteLine();
        
        // Find matches using case-sensitive regular expression.
        matches = rxSensitive.Matches(text);

        // Report the number of matches found.
        Console.WriteLine("{0} matches found in:\n   {1}", 
                          matches.Count, 
                          text);

        // Report on each match.
        foreach (Match match in matches)
        {
            GroupCollection groups = match.Groups;
            Console.WriteLine("'{0}' repeated at positions {1} and {2}",  
                              groups["word"].Value, 
                              groups[0].Index, 
                              groups[1].Index);
        }
    }
}
// The example produces the following output to the console:
//       3 matches found in:
//          The the quick brown fox  fox jumps over the lazy dog dog.
//       'The' repeated at positions 0 and 4
//       'fox' repeated at positions 20 and 25
//       'dog' repeated at positions 50 and 54
//       
//       2 matches found in:
//          The the quick brown fox  fox jumps over the lazy dog dog.
//       'fox' repeated at positions 20 and 25
//       'dog' repeated at positions 50 and 54
Imports System.Text.RegularExpressions

Public Module Test

    Public Sub Main()
        ' Define a case-insensitive regular expression for repeated words.
        Dim rxInsensitive As New Regex("\b(?<word>\w+)\s+(\k<word>)\b", _
               RegexOptions.Compiled Or RegexOptions.IgnoreCase)
        ' Define a case-sensitive regular expression for repeated words.
        Dim rxSensitive As New Regex("\b(?<word>\w+)\s+(\k<word>)\b", _
               RegexOptions.Compiled)

        ' Define a test string.        
        Dim text As String = "The the quick brown fox  fox jumps over the lazy dog dog."
        
        ' Find matches using case-insensitive regular expression.
        Dim matches As MatchCollection = rxInsensitive.Matches(text)

        ' Report the number of matches found.
        Console.WriteLine("{0} matches found in:", matches.Count)
        Console.WriteLine("   {0}", text)

        ' Report on each match.
        For Each match As Match In matches
            Dim groups As GroupCollection = match.Groups
            Console.WriteLine("'{0}' repeated at positions {1} and {2}", _ 
                              groups.Item("word").Value, _
                              groups.Item(0).Index, _
                              groups.Item(1).Index)
        Next
        Console.WriteLine()
        
        ' Find matches using case-sensitive regular expression.
        matches = rxSensitive.Matches(text)

        ' Report the number of matches found.
        Console.WriteLine("{0} matches found in:", matches.Count)
        Console.WriteLine("   {0}", text)

        ' Report on each match.
        For Each match As Match In matches
            Dim groups As GroupCollection = match.Groups
            Console.WriteLine("'{0}' repeated at positions {1} and {2}", _ 
                              groups.Item("word").Value, _
                              groups.Item(0).Index, _
                              groups.Item(1).Index)
        Next
        Console.WriteLine()
    End Sub
End Module
' The example produces the following output to the console:
'       3 matches found in:
'          The the quick brown fox  fox jumps over the lazy dog dog.
'       'The' repeated at positions 0 and 4
'       'fox' repeated at positions 20 and 25
'       'dog' repeated at positions 50 and 54
'       
'       2 matches found in:
'          The the quick brown fox  fox jumps over the lazy dog dog.
'       'fox' repeated at positions 20 and 25
'       'dog' repeated at positions 50 and 54

備註

RegexOptions您可以將值當做 參數提供給 類別的 Regex 下列成員:

RegexOptions您也可以將值當做參數 RegexCompilationInfo 提供給建構函式,也可以直接指派給 RegexCompilationInfo.Options 屬性。 然後,產生的 RegexCompilationInfo 物件會在呼叫 Regex.CompileToAssembly 方法時使用。

列舉成員 RegexOptions 所提供的數個選項 (,其 ExplicitCaptureIgnoreCaseMultilineSingleline 成員) 可以改為使用正則運算式模式中的內嵌選項字元來提供。 如需詳細資訊,請參閱 正則運算式選項

適用於

另請參閱