通过


RegexOptions 枚举

定义

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

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

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

字段

名称 说明
None 0

指定未设置任何选项。 有关正则表达式引擎的默认行为的详细信息,请参阅 正则表达式选项 文章中的“默认选项”部分。

IgnoreCase 1

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

Multiline 2

多行模式。 更改 ^ 和 $ 的含义,以便它们分别在任何行的开头和结尾匹配,而不仅仅是整个字符串的开头和结尾。 有关详细信息,请参阅 正则表达式选项 文章中的“多行模式”部分。

ExplicitCapture 4

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

Compiled 8

指定正则表达式编译为 MSIL 代码,而不是被解释。 已编译的正则表达式最大限度地提高运行时性能,代价是会影响初始化时间。 调用CompileToAssembly(RegexCompilationInfo[], AssemblyName)方法时,不应将此值分配给Options属性。 有关详细信息,请参阅 正则表达式选项 文章中的“已编译正则表达式”部分。

Singleline 16

指定单行模式。 更改点(.)的含义,使其匹配每个字符(而不是除 \n以外的每个字符)。 有关详细信息,请参阅 正则表达式选项 文章中的“单行模式”部分。

IgnorePatternWhitespace 32

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

RightToLeft 64

指定搜索将从右到左,而不是从左到右。 有关详细信息,请参阅 正则表达式选项 文章中的“从右向左模式”部分。

ECMAScript 256

为表达式启用符合 ECMAScript 的行为。 此值只能与值MultilineCompiledIgnoreCase值结合使用。 将此值与任何其他值一起使用会导致异常。

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

CultureInvariant 512

指定忽略语言中的文化差异。 有关详细信息,请参阅 正则表达式选项 文章中的“使用固定区域性进行比较”部分。

NonBacktracking 1024

使用避免回溯的方法启用匹配,并保证输入长度的线性时间处理。

示例

下面的示例定义了两个正则表达式,这些正则表达式标识文本中的重复单词,但使用不同 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对象。

枚举成员提供的多个选项(特别是枚举成员RegexOptionsMultilineIgnoreCase成员ExplicitCaptureSingleline成员)可以通过在正则表达式模式中使用内联选项字符来提供。 有关详细信息,请参阅 正则表达式选项

适用于

另请参阅