RegexOptions 枚举

定义

提供用于设置正则表达式选项的枚举值。

此枚举支持其成员值的按位组合。

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 值一起使用。 该值与其他任何值一起使用均将导致异常。

有关 ECMAScript 选项的详细信息,请参阅正则表达式选项文章中的“ECMAScript 匹配行为”部分。

ExplicitCapture 4

指定唯一有效的捕获是显式命名或编号的 (?<name>...) 形式的组。这使未命名的圆括号可以充当非捕获组,并且不会使表达式的语法 (?:...) 显得笨拙。有关详细信息,请参阅正则表达式选项文章中的“仅显式捕获”部分。

IgnoreCase 1

指定不区分大小写的匹配。 有关详细信息,请参阅正则表达式选项文章中的“不区分大小写匹配”部分。

IgnorePatternWhitespace 32

消除模式中的非转义空白并启用由 # 标记的注释。 但是,此值不影响或消除标记单独的正则表达式语言元素的开头的字符类、数值量词或标记的空格。 有关详细信息,请参阅正则表达式选项一文中的“忽略空格”部分。

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将其分配给 属性。 然后,在调用 Regex.CompileToAssembly 方法时使用生成的 RegexCompilationInfo 对象。

枚举成员 RegexOptions 提供的多个选项尤其 (,其 ExplicitCaptureIgnoreCaseMultilineSingleline 成员) 可以改为使用正则表达式模式中的内联选项字符来提供。 有关详细信息,请参阅 正则表达式选项

适用于

另请参阅