RegexOptions 列舉

定義

提供用來設定規則運算式 (Regular Expression) 選項的列舉值。Provides enumerated values to use to set regular expression options.

此列舉具有 FlagsAttribute 個屬性允許以位元方式合併其成員值。

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

欄位

Compiled 8

指定規則運算式要編譯為組件,Specifies that the regular expression is compiled to an assembly. 這將產生較快速的執行,但會增加啟動時間。This yields faster execution but increases startup time. 在呼叫 Options 方法時,不應將這個值指派至 CompileToAssembly(RegexCompilationInfo[], AssemblyName) 屬性。This value should not be assigned to the Options property when calling the CompileToAssembly(RegexCompilationInfo[], AssemblyName) method. 如需詳細資訊,請參閱規則運算式選項主題的<編譯的規則運算式>一節。For more information, see the "Compiled Regular Expressions" section in the Regular Expression Options topic.

CultureInvariant 512

指定忽略語言中的文化差異,Specifies that cultural differences in language is ignored. 如需詳細資訊,請參閱規則運算式選項主題的<使用不因文化特性而異的比較>一節。For more information, see the "Comparison Using the Invariant Culture" section in the Regular Expression Options topic.

ECMAScript 256

啟用運算式的 ECMAScript 相容行為,Enables ECMAScript-compliant behavior for the expression. 這個值只能結合 IgnoreCaseMultilineCompiled 值使用,This value can be used only in conjunction with the IgnoreCase, Multiline, and Compiled values. 將這個值與任何其他值一起使用都將導致例外狀況。The use of this value with any other values results in an exception.

如需 ECMAScript 選項的詳細資訊,請參閱規則運算式選項主題的<ECMAScript 比對行為>一節。For more information on the ECMAScript option, see the "ECMAScript Matching Behavior" section in the Regular Expression Options topic.

ExplicitCapture 4

指定唯一有效的擷取為明確命名或編號之格式為 (?<名稱>...) 的群組。這允許未命名的括號充當非擷取群組,而避免運算式 (?:...) 的語法不便。如需詳細資訊,請參閱規則運算式選項主題的<僅明確擷取>一節。Specifies that the only valid captures are explicitly named or numbered groups of the form (?<name>...). This allows unnamed parentheses to act as noncapturing groups without the syntactic clumsiness of the expression (?:...). For more information, see the "Explicit Captures Only" section in the Regular Expression Options topic.

IgnoreCase 1

指定區分大小寫的比對。Specifies case-insensitive matching. 如需詳細資訊,請參閱規則運算式選項主題的<不區分大小寫比對>一節。For more information, see the "Case-Insensitive Matching " section in the Regular Expression Options topic.

IgnorePatternWhitespace 32

從模式排除未逸出的泛空白字元 (White Space),並啟用以 # 標記的註解。Eliminates unescaped white space from the pattern and enables comments marked with #. 不過,這個值不會影響或排除字元類別中的空白字元、數值數量詞,或是標示個別規則運算式語言項目開始的權杖。However, this value does not affect or eliminate white space in character classes, numeric quantifiers, or tokens that mark the beginning of individual regular expression language elements. 如需詳細資訊,請參閱規則運算式選項主題的<忽略空白字元>一節。For more information, see the "Ignore White Space" section of the Regular Expression Options topic.

Multiline 2

多行模式。Multiline mode. 變更 ^ 和 $ 的意義以致它們分別在任何一行的開頭和結尾做比對,而不只是整個字串的開頭和結尾。Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string. 如需詳細資訊,請參閱規則運算式選項主題的<多行模式>一節。For more information, see the "Multiline Mode" section in the Regular Expression Options topic.

None 0

指定無選項設定。Specifies that no options are set. 如需規則運算式引擎預設行為的詳細資訊,請參閱規則運算式選項主題的<預設選項>一節。For more information about the default behavior of the regular expression engine, see the "Default Options" section in the Regular Expression Options topic.

RightToLeft 64

指定搜尋將由右至左,而非由左至右。Specifies that the search will be from right to left instead of from left to right. 如需詳細資訊,請參閱規則運算式選項主題的<由右至左模式>一節。For more information, see the "Right-to-Left Mode" section in the Regular Expression Options topic.

Singleline 16

指定單行模式,Specifies single-line mode. 變更點 (.) 的意義,使它符合一切字元 (而不是 \n 之外的一切字元)。Changes the meaning of the dot (.) so it matches every character (instead of every character except \n). 如需詳細資訊,請參閱規則運算式選項主題的<單行模式>一節。For more information, see the "Single-line Mode" section in the Regular Expression Options topic.

範例

下列範例會定義兩個正則運算式, 用來識別文字中的重複文字, RegexOptions但使用不同的值具現化。The following example defines two regular expressions that identify repeated words in text but that are instantiated using different RegexOptions values. 第一個正則運算式不區分大小寫;判斷單字是否與上一個字相同時, 會忽略大小寫。The first regular expression is case-insensitive; case is ignored when determining whether a word is identical to the preceding word. 第二個正則運算式會區分大小寫;單字必須符合前面單字的大小寫, 才能視為重複。The second regular expression is case-sensitive; a word must match the case of the preceding word exactly to be considered a duplicate.

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

備註

值可以當做參數提供給Regex類別的下列成員: RegexOptionsA RegexOptions value can be provided as a parameter to the following members of the Regex class:

值也可以當做參數提供RegexCompilationInfo給函式, 也可以直接RegexCompilationInfo.Options指派給屬性。 RegexOptionsA RegexOptions value can also be supplied as a parameter to the RegexCompilationInfo constructor, or it can be assigned directly to the RegexCompilationInfo.Options property. 然後, RegexCompilationInfo會在Regex.CompileToAssembly方法的呼叫中使用產生的物件。The resulting RegexCompilationInfo object is then used in the call to the Regex.CompileToAssembly method.

RegexOptions列舉的成員提供的數個選項 (特別是透過其ExplicitCapture、、 IgnoreCase MultilineSingleline成員), 可以改為使用正則運算式中的內嵌選項字元來提供pattern.Several options provided by members of the RegexOptions enumeration (in particular, by its ExplicitCapture, IgnoreCase, Multiline, and Singleline members) can instead be provided by using an inline option character in the regular expression pattern. 如需詳細資訊, 請參閱正則運算式選項For details, see Regular Expression Options.

適用於

另請參閱