RegexOptions RegexOptions RegexOptions RegexOptions Enum

定义

提供用于设置正则表达式选项的枚举值。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
继承
属性

字段

Compiled Compiled Compiled 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 CultureInvariant CultureInvariant 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 ECMAScript ECMAScript 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 ExplicitCapture ExplicitCapture ExplicitCapture 4

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

指定不区分大小写的匹配。Specifies case-insensitive matching. 有关详细信息,请参见 正则表达式选项主题中的“不区分大小写匹配”部分。For more information, see the "Case-Insensitive Matching " section in the Regular Expression Options topic.

IgnorePatternWhitespace IgnorePatternWhitespace IgnorePatternWhitespace IgnorePatternWhitespace 32

消除模式中的非转义空白并启用由 # 标记的注释。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 Multiline Multiline 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 None None 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 RightToLeft RightToLeft 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 Singleline Singleline 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使用枚举成员提供的多个选项 (特别是由其、 IgnoreCaseMultilineSingleline成员提供)化.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.

适用于

另请参阅