Regex 类

定义

表示不可变的正则表达式。

public ref class Regex
public ref class Regex : System::Runtime::Serialization::ISerializable
public class Regex
public class Regex : System.Runtime.Serialization.ISerializable
[System.Serializable]
public class Regex : System.Runtime.Serialization.ISerializable
type Regex = class
type Regex = class
    interface ISerializable
[<System.Serializable>]
type Regex = class
    interface ISerializable
Public Class Regex
Public Class Regex
Implements ISerializable
继承
Regex
派生
属性
实现

示例

以下示例使用正则表达式来检查字符串中重复出现的单词。 可以解释正则表达式 \b(?<word>\w+)\s+(\k<word>)\b ,如下表所示。

模式 说明
\b 在单词边界处开始匹配。
(?<word>\w+) 将一个或多个单词字符匹配到一个单词边界。 将此捕获的组 word命名为 。
\s+ 匹配一个或多个空白字符。
(\k<word>) 匹配名为 的 word捕获组。
\b 与字边界匹配。
#using <System.dll>

using namespace System;
using namespace System::Text::RegularExpressions;
int main()
{
   // Define a regular expression for repeated words.
   Regex^ rx = gcnew Regex( "\\b(?<word>\\w+)\\s+(\\k<word>)\\b",static_cast<RegexOptions>(RegexOptions::Compiled | RegexOptions::IgnoreCase) );

   // Define a test string.        
   String^ text = "The the quick brown fox  fox jumps over the lazy dog dog.";

   // Find matches.
   MatchCollection^ matches = rx->Matches( text );

   // Report the number of matches found.
   Console::WriteLine( "{0} matches found.", matches->Count );

   // Report on each match.
   for each (Match^ match in matches)
   {
      String^ word = match->Groups["word"]->Value;
      int index = match->Index;
      Console::WriteLine("{0} repeated at position {1}", word, index);   
   }
}
using System;
using System.Text.RegularExpressions;

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

        // Define a test string.
        string text = "The the quick brown fox  fox jumps over the lazy dog dog.";

        // Find matches.
        MatchCollection matches = rx.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 49 and 53
Imports System.Text.RegularExpressions

Public Module Test

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

        ' Define a test string.        
        Dim text As String = "The the quick brown fox  fox jumps over the lazy dog dog."
        
        ' Find matches.
        Dim matches As MatchCollection = rx.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
    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 49 and 53

以下示例演示了如何使用正则表达式来检查字符串是表示货币值还是具有表示货币值的正确格式。 在这种情况下,正则表达式是从 en-US 区域性的 NumberFormatInfo.CurrencyDecimalSeparatorCurrencyDecimalDigitsNumberFormatInfo.CurrencySymbolNumberFormatInfo.NegativeSignNumberFormatInfo.PositiveSign 属性动态生成的。 生成的正则表达式为 ^\s*[\+-]?\s?\$?\s?(\d*\.?\d{2}?){1}$。 此正则表达式可以解释为下表所示。

模式 说明
^ 从字符串的开头开始。
\s* 匹配零个或多个空白字符。
[\+-]? 匹配正号或负号的零个或一个匹配项。
\s? 匹配零个或一个空白字符。
\$? 匹配美元符号的零个或一个匹配项。
\s? 匹配零个或一个空白字符。
\d* 匹配零个或多个十进制数字。
\.? 匹配零或一个小数点符号。
(\d{2})? 捕获组 1:匹配两个十进制数字零或一次。
(\d*\.?(\d{2})?){1} 匹配由小数点符号分隔的整数和小数位数模式至少一次。
$ 匹配字符串的末尾。

在这种情况下,正则表达式假定有效的货币字符串不包含组分隔符符号,并且它没有小数位数或由指定区域性的 CurrencyDecimalDigits 属性定义的小数位数。

using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        // Get the en-US NumberFormatInfo object to build the regular 
        // expression pattern dynamically.
        NumberFormatInfo nfi = CultureInfo.GetCultureInfo("en-US").NumberFormat;

        // Define the regular expression pattern.
        string pattern;
        pattern = @"^\s*[";
        // Get the positive and negative sign symbols.
        pattern += Regex.Escape(nfi.PositiveSign + nfi.NegativeSign) + @"]?\s?";
        // Get the currency symbol.
        pattern += Regex.Escape(nfi.CurrencySymbol) + @"?\s?";
        // Add integral digits to the pattern.
        pattern += @"(\d*";
        // Add the decimal separator.
        pattern += Regex.Escape(nfi.CurrencyDecimalSeparator) + "?";
        // Add the fractional digits.
        pattern += @"(\d{";
        // Determine the number of fractional digits in currency values.
        pattern += nfi.CurrencyDecimalDigits.ToString() + "})?){1}$";

        Console.WriteLine($"Pattern is {pattern}\n");

        Regex rgx = new Regex(pattern);

        // Define some test strings.
        string[] tests = { "-42", "19.99", "0.001", "100 USD",
                         ".34", "0.34", "1,052.21", "$10.62",
                         "+1.43", "-$0.23" };

        // Check each test string against the regular expression.
        foreach (string test in tests)
        {
            if (rgx.IsMatch(test))
                Console.WriteLine($"{test} is a currency value.");
            else
                Console.WriteLine($"{test} is not a currency value.");
        }
    }
}
// The example displays the following output:
//       Pattern is ^\s*[\+-]?\s?\$?\s?(\d*\.?(\d{2})?){1}$
//
//       -42 is a currency value.
//       19.99 is a currency value.
//       0.001 is not a currency value.
//       100 USD is not a currency value.
//       .34 is a currency value.
//       0.34 is a currency value.
//       1,052.21 is not a currency value.
//       $10.62 is a currency value.
//       +1.43 is a currency value.
//       -$0.23 is a currency value.
Imports System.Globalization
Imports System.Text.RegularExpressions

Public Module Example
   Public Sub Main()
      ' Get the current NumberFormatInfo object to build the regular 
      ' expression pattern dynamically.
      Dim nfi As NumberFormatInfo = CultureInfo.GetCultureInfo("en-US").NumberFormat

      ' Define the regular expression pattern.
      Dim pattern As String 
      pattern = "^\s*["
      ' Get the positive and negative sign symbols.
      pattern += Regex.Escape(nfi.PositiveSign + nfi.NegativeSign) + "]?\s?"
      ' Get the currency symbol.
      pattern += Regex.Escape(nfi.CurrencySymbol) + "?\s?"
      ' Add integral digits to the pattern.
      pattern += "(\d*"
      ' Add the decimal separator.
      pattern += Regex.Escape(nfi.CurrencyDecimalSeparator) + "?"
      ' Add the fractional digits.
      pattern += "(\d{"
      ' Determine the number of fractional digits in currency values.
      pattern += nfi.CurrencyDecimalDigits.ToString() + "})?){1}$"
      
      Console.WriteLine("Pattern is {0}", pattern)
      Console.WriteLine()
      
      Dim rgx As New Regex(pattern)

      ' Define some test strings.
      Dim tests() As String = {"-42", "19.99", "0.001", "100 USD", _
                               ".34", "0.34", "1,052.21", "$10.62", _
                               "+1.43", "-$0.23" }

      ' Check each test string against the regular expression.
      For Each test As String In tests
         If rgx.IsMatch(test) Then
            Console.WriteLine("{0} is a currency value.", test)
         Else
            Console.WriteLine("{0} is not a currency value.", test)
         End If
      Next
   End Sub
End Module
' The example displays the following output:
'       Pattern is ^\s*[\+-]?\s?\$?\s?(\d*\.?(\d{2})?){1}$
'
'       -42 is a currency value.
'       19.99 is a currency value.
'       0.001 is not a currency value.
'       100 USD is not a currency value.
'       .34 is a currency value.
'       0.34 is a currency value.
'       1,052.21 is not a currency value.
'       $10.62 is a currency value.
'       +1.43 is a currency value.
'       -$0.23 is a currency value.

由于此示例中的正则表达式是动态生成的,因此在设计时,你不知道指定区域性的货币符号、十进制符号或正负符号 (本示例中) 可能被正则表达式引擎错误解释为正则表达式语言运算符。 为了防止任何错误解释,该示例将每个动态生成的字符串传递给 Escape 方法。

注解

Regex表示.NET Framework的正则表达式引擎。 它可用于快速分析大量文本以查找特定的字符模式:提取、编辑、替换或删除文本子字符串;和 ,将提取的字符串添加到集合以生成报表。

注意

如果主要兴趣是通过确定字符串是否符合特定模式来验证字符串,则可以使用 System.Configuration.RegexStringValidator 类。

若要使用正则表达式,请使用 正则表达式语言 - 快速参考中记录的语法来定义要在文本流中标识的模式。 接下来,可以选择实例化 Regex 对象。 最后,调用执行某些操作的方法,例如替换与正则表达式模式匹配的文本,或标识模式匹配。

注意

有关一些常见的正则表达式模式,请参阅 正则表达式示例。 还有一些正则表达式模式的联机库,例如 位于 Regular-Expressions.info

有关使用 Regex 类的详细信息,请参阅本主题中的以下部分:

若要详细了解正则表达式语言,请参阅正则表达式语言 - 快速参考,或下载和打印下面的小册子之一:

快速参考(Word (.docx) 格式)
快速参考(PDF (.pdf) 格式)

正则表达式与字符串方法

System.String 包括多个搜索和比较方法,可用于执行与文本的模式匹配。 例如,、 和 方法确定字符串实例是否包含指定的子字符串;、 String.IndexOfString.LastIndexOfString.IndexOfAnyString.LastIndexOfAny 方法返回字符串中指定子字符串的起始位置。String.StartsWithString.EndsWithString.Contains 搜索特定字符串时, System.String 请使用 类的方法。 Regex在字符串中搜索特定模式时,请使用 类。 有关详细信息和示例,请参阅 .NET 正则表达式

返回到“备注”

静态方法与实例方法

定义正则表达式模式后,可以通过以下两种方式之一将其提供给正则表达式引擎:

  • 通过实例化 Regex 表示正则表达式的 对象。 为此,请将正则表达式模式传递给 Regex 构造函数。 对象 Regex 是不可变的;使用正则表达式实例化 Regex 对象时,无法更改该对象的正则表达式。

  • 通过提供正则表达式和文本以 static 在 Visual Basic) 方法中搜索 (SharedRegex 。 这使你无需显式创建 Regex 对象即可使用正则表达式。

所有 Regex 模式标识方法都包括静态重载和实例重载。

正则表达式引擎必须先编译特定模式,然后才能使用该模式。 由于 Regex 对象是不可变的,因此这是调用类构造函数或静态方法时 Regex 发生的一次性过程。 为了消除重复编译单个正则表达式的需要,正则表达式引擎会缓存静态方法调用中使用的已编译正则表达式。 因此,正则表达式模式匹配方法为静态方法和实例方法提供可比的性能。

重要

在.NET Framework版本 1.0 和 1.1 中,所有编译的正则表达式(无论是在实例调用还是静态方法调用中使用)都已缓存。 从 .NET Framework 2.0 开始,仅缓存静态方法调用中使用的正则表达式。

但是,在以下两种情况下,缓存可能会对性能产生负面影响:

  • 使用具有大量正则表达式的静态方法调用时。 默认情况下,正则表达式引擎缓存最近使用的 15 个静态正则表达式。 如果应用程序使用超过 15 个静态正则表达式,则必须重新编译某些正则表达式。 若要防止这种重新编译,可以增加 Regex.CacheSize 属性。

  • 使用以前编译的正则表达式实例化新 Regex 对象时。 例如,以下代码定义一个正则表达式来查找文本流中的重复字词。 尽管该示例使用单个正则表达式,但它实例化一个新的 Regex 对象来处理每行文本。 这会导致每次循环迭代时重新编译正则表达式。

    StreamReader sr = new StreamReader(filename);
    string input;
    string pattern = @"\b(\w+)\s\1\b";
    while (sr.Peek() >= 0)
    {
       input = sr.ReadLine();
       Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
       MatchCollection matches = rgx.Matches(input);
       if (matches.Count > 0)
       {
          Console.WriteLine("{0} ({1} matches):", input, matches.Count);
          foreach (Match match in matches)
             Console.WriteLine("   " + match.Value);
       }
    }
    sr.Close();
    
    Dim sr As New StreamReader(filename)
    Dim input As String
    Dim pattern As String = "\b(\w+)\s\1\b"
    Do While sr.Peek() >= 0
       input = sr.ReadLine()
       Dim rgx As New Regex(pattern, RegexOptions.IgnoreCase)
       Dim matches As MatchCollection = rgx.Matches(input)
       If matches.Count > 0 Then
          Console.WriteLine("{0} ({1} matches):", input, matches.Count)
          For Each match As Match In matches
             Console.WriteLine("   " + match.Value)
          Next   
       End If
    Loop
    sr.Close()
    

    若要防止重新编译,应实例化一个可供需要重新编译的代码访问的单个 Regex 对象,如以下重写示例所示。

    StreamReader sr = new StreamReader(filename);
    string input;
    string pattern = @"\b(\w+)\s\1\b";
    Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
    
    while (sr.Peek() >= 0)
    {
       input = sr.ReadLine();
       MatchCollection matches = rgx.Matches(input);
       if (matches.Count > 0)
       {
          Console.WriteLine("{0} ({1} matches):", input, matches.Count);
          foreach (Match match in matches)
             Console.WriteLine("   " + match.Value);
       }
    }
    sr.Close();
    
    Dim sr As New StreamReader(filename)
    Dim input As String
    Dim pattern As String = "\b(\w+)\s\1\b"
    Dim rgx As New Regex(pattern, RegexOptions.IgnoreCase)
    Do While sr.Peek() >= 0
       input = sr.ReadLine()
       Dim matches As MatchCollection = rgx.Matches(input)
       If matches.Count > 0 Then
          Console.WriteLine("{0} ({1} matches):", input, matches.Count)
          For Each match As Match In matches
             Console.WriteLine("   " + match.Value)
          Next   
       End If
    Loop
    sr.Close()
    

返回到“备注”

执行正则表达式操作

无论你决定实例化 Regex 对象并调用其方法还是调用静态方法, Regex 类都提供以下模式匹配功能:

  • 匹配项的验证。 调用 IsMatch 方法以确定是否存在匹配项。

  • 检索单个匹配项。 调用 Match 方法以检索 Match 表示字符串或部分字符串中的第一个匹配项的 对象。 可以通过调用 Match.NextMatch 方法检索后续匹配项。

  • 检索所有匹配项。 调用 Matches 方法以检索对象 System.Text.RegularExpressions.MatchCollection ,该对象表示在字符串或字符串的一部分中找到的所有匹配项。

  • 替换匹配的文本。 调用 方法以 Replace 替换匹配的文本。 替换文本也可以由正则表达式定义。 此外,某些 Replace 方法包括一个 MatchEvaluator 参数,使你能够以编程方式定义替换文本。

  • 创建由输入字符串的各个部分构成的字符串数组。 调用 方法以 Split 在正则表达式定义的位置拆分输入字符串。

除了模式匹配方法之外 Regex , 类还包括几个特殊用途的方法:

  • 方法 Escape 将转义在正则表达式或输入字符串中可能被解释为正则表达式运算符的任何字符。

  • 方法 Unescape 删除这些转义字符。

  • 方法 CompileToAssembly 创建包含预定义正则表达式的程序集。 .NET Framework包含 命名空间中System.Web.RegularExpressions这些特殊用途程序集的示例。

返回到“备注”

定义Time-Out值

.NET 支持功能齐全的正则表达式语言,该语言在模式匹配方面提供强大的功能和灵活性。 但是,其功能和灵活性是有代价的:性能不佳的风险。 性能不佳的正则表达式非常易于创建。 在某些情况下,依赖于过度回溯的正则表达式操作在处理与正则表达式模式几乎匹配的文本时,似乎会停止响应。 有关 .NET 正则表达式引擎的详细信息,请参阅 正则表达式行为的详细信息。 有关过度回溯的详细信息,请参阅 回溯

从 .NET Framework 4.5 开始,可以为正则表达式匹配定义超时间隔,以限制过度回溯。 根据正则表达式模式和输入文本,执行时间可能会超过指定的超时间隔,但它不会花费比指定的超时间隔更多的时间回溯。 如果正则表达式引擎超时,则会引发 RegexMatchTimeoutException 异常。 在大多数情况下,这可以防止正则表达式引擎通过尝试匹配几乎与正则表达式模式匹配的文本来浪费处理能力。 但是,它还可能表明超时间隔设置得太低,或者当前计算机负载导致整体性能下降。

如何处理异常取决于异常的原因。 如果由于超时间隔设置太低或计算机负载过大而发生异常,则可以增加超时间隔并重试匹配操作。 如果由于正则表达式依赖于过度回溯而发生异常,则可以假定不存在匹配项,并且可以选择性地记录有助于修改正则表达式模式的信息。

实例化正则表达式对象时,可以通过调用 Regex(String, RegexOptions, TimeSpan) 构造函数来设置超时间隔。 对于静态方法,可以通过调用具有 matchTimeout 参数的匹配方法的重载来设置超时间隔。 如果未显式设置超时值,则按如下所示确定默认超时值:

  • 使用应用程序范围的超时值(如果存在)。 这可以是适用于应用程序域(在其中实例化 Regex 对象或进行静态方法调用)的任何超时值。 可以通过调用 AppDomain.SetData 方法将 TimeSpan 值的字符串表示形式分配给“REGEX_DEFAULT_MATCH_TIMEOUT”属性,从而设置应用程序范围的超时值。

  • 使用值 InfiniteMatchTimeout(如果未设置应用程序范围的超时值)。

重要

建议在所有正则表达式模式匹配操作中设置超时值。 有关详细信息,请参阅 正则表达式的最佳做法

返回到“备注”

构造函数

Regex()

初始化 Regex 类的新实例。

Regex(SerializationInfo, StreamingContext)
已过时。

使用序列化数据初始化 Regex 类的新实例。

Regex(String)

为指定的正则表达式初始化 Regex 类的新实例。

Regex(String, RegexOptions)

使用修改模式的选项为指定的正则表达式初始化 Regex 类的新实例。

Regex(String, RegexOptions, TimeSpan)

使用修改模式的选项和指定在超时前多久模式匹配方法应进行匹配尝试的值为指定正则表达式初始化 Regex 类的新实例。

字段

capnames

CompileToAssembly 方法生成的 Regex 对象使用。

caps

CompileToAssembly 方法生成的 Regex 对象使用。

capsize

CompileToAssembly 方法生成的 Regex 对象使用。

capslist

CompileToAssembly 方法生成的 Regex 对象使用。

factory

CompileToAssembly 方法生成的 Regex 对象使用。

InfiniteMatchTimeout

指定模式匹配操作不应超时。

internalMatchTimeout

操作超时之前在一个模式匹配操作中可以经过的最长时间。

pattern

CompileToAssembly 方法生成的 Regex 对象使用。

roptions

CompileToAssembly 方法生成的 Regex 对象使用。

属性

CacheSize

获取或设置已编译的正则表达式的当前静态缓存中的最大项数。

CapNames

获取或设置将命名捕获组映射到其索引值的字典。

Caps

获取或设置将编号捕获组映射到其索引值的字典。

MatchTimeout

获取当前实例的超时间隔。

Options

获取传递给 Regex 构造函数的选项。

RightToLeft

获取一个值,该值指示正则表达式是否从右向左进行搜索。

方法

CompileToAssembly(RegexCompilationInfo[], AssemblyName)
已过时。

将一个或多个指定的 Regex 对象编译为命名程序集。

CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[])
已过时。

将一个或多个指定的 Regex 对象编译为具有指定特性的命名程序集。

CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String)
已过时。

将一个或多个指定的 Regex 对象和一个指定的资源文件编译为具有指定特性的命名程序集。

Count(ReadOnlySpan<Char>)

在输入范围中搜索正则表达式的所有匹配项,并返回匹配项数。

Count(ReadOnlySpan<Char>, Int32)

在输入范围中搜索正则表达式的所有匹配项,并返回匹配项数。

Count(ReadOnlySpan<Char>, String)

在输入范围中搜索正则表达式的所有匹配项,并返回匹配项数。

Count(ReadOnlySpan<Char>, String, RegexOptions)

在输入范围中搜索正则表达式的所有匹配项,并返回匹配项数。

Count(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)

在输入范围中搜索正则表达式的所有匹配项,并返回匹配项数。

Count(String)

在输入字符串中搜索正则表达式的所有匹配项,并返回匹配项数。

Count(String, String)

在输入字符串中搜索正则表达式的所有匹配项,并返回匹配项数。

Count(String, String, RegexOptions)

在输入字符串中搜索正则表达式的所有匹配项,并返回匹配项数。

Count(String, String, RegexOptions, TimeSpan)

在输入字符串中搜索正则表达式的所有匹配项,并返回匹配项数。

EnumerateMatches(ReadOnlySpan<Char>)

在输入范围中搜索正则表达式的所有匹配项,并返回 以 Regex.ValueMatchEnumerator 循环访问匹配项。

EnumerateMatches(ReadOnlySpan<Char>, Int32)

在输入范围中搜索正则表达式的所有匹配项,并返回 以 Regex.ValueMatchEnumerator 循环访问匹配项。

EnumerateMatches(ReadOnlySpan<Char>, String)

在输入范围中搜索正则表达式的所有匹配项,并返回 以 Regex.ValueMatchEnumerator 循环访问匹配项。

EnumerateMatches(ReadOnlySpan<Char>, String, RegexOptions)

在输入范围中搜索正则表达式的所有匹配项,并返回 以 Regex.ValueMatchEnumerator 循环访问匹配项。

EnumerateMatches(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)

在输入范围中搜索正则表达式的所有匹配项,并返回 以 Regex.ValueMatchEnumerator 循环访问匹配项。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
Escape(String)

将一组最小字符 (\、*、+、?、|、{、[、 (、) 、^、$、.、#和空格) 转义。 这将指示正则表达式引擎按原义解释这些字符而不是解释为元字符。

Finalize()

此成员替代 Finalize() 且该主题可能包括更完整的文档。

允许 Object 在"垃圾回收"回收 Object 之前尝试释放资源并执行其他清理操作。

GetGroupNames()

返回正则表达式的捕获组名数组。

GetGroupNumbers()

返回与数组中的组名相对应的捕获组号的数组。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
GroupNameFromNumber(Int32)

获取与指定组号相对应的组名。

GroupNumberFromName(String)

返回与指定组名相对应的组号。

InitializeReferences()
已过时。

CompileToAssembly 方法生成的 Regex 对象使用。

IsMatch(ReadOnlySpan<Char>)

指示 Regex 构造函数中指定的正则表达式是否在指定的输入范围中找到匹配项。

IsMatch(ReadOnlySpan<Char>, Int32)

指示 Regex 构造函数中指定的正则表达式是否在指定的输入范围中找到匹配项。

IsMatch(ReadOnlySpan<Char>, String)

指示指定的正则表达式是否在指定的输入范围中查找匹配项。

IsMatch(ReadOnlySpan<Char>, String, RegexOptions)

指示指定的正则表达式是否使用指定的匹配选项在指定的输入范围中查找匹配项。

IsMatch(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)

指示指定的正则表达式是否使用指定的匹配选项和超时间隔在指定的输入范围中查找匹配项。

IsMatch(String)

指示 Regex 构造函数中指定的正则表达式在指定的输入字符串中是否找到了匹配项。

IsMatch(String, Int32)

指示 Regex 构造函数中指定的正则表达式在指定的输入字符串中,从该字符串中的指定起始位置开始是否找到了匹配项。

IsMatch(String, String)

指示所指定的正则表达式在指定的输入字符串中是否找到了匹配项。

IsMatch(String, String, RegexOptions)

指示所指定的正则表达式是否使用指定的匹配选项在指定的输入字符串中找到了匹配项。

IsMatch(String, String, RegexOptions, TimeSpan)

指示所指定的正则表达式是否使用指定的匹配选项和超时间隔在指定的输入字符串中找到了匹配项。

Match(String)

在指定的输入字符串中搜索 Regex 构造函数中指定的正则表达式的第一个匹配项。

Match(String, Int32)

从输入字符串中的指定起始位置开始,在该字符串中搜索正则表达式的第一个匹配项。

Match(String, Int32, Int32)

从指定的起始位置开始,在输入字符串中搜索正则表达式的第一个匹配项,并且仅搜索指定数量的字符。

Match(String, String)

在指定的输入字符串中搜索指定的正则表达式的第一个匹配项。

Match(String, String, RegexOptions)

使用指定的匹配选项在输入字符串中搜索指定的正则表达式的第一个匹配项。

Match(String, String, RegexOptions, TimeSpan)

使用指定的匹配选项和超时间隔在输入字符串中搜索指定的正则表达式的第一个匹配项。

Matches(String)

在指定的输入字符串中搜索正则表达式的所有匹配项。

Matches(String, Int32)

从字符串中的指定起始位置开始,在指定的输入字符串中搜索正则表达式的所有匹配项。

Matches(String, String)

在指定的输入字符串中搜索指定的正则表达式的所有匹配项。

Matches(String, String, RegexOptions)

使用指定的匹配选项在指定的输入字符串中搜索指定的正则表达式的所有匹配项。

Matches(String, String, RegexOptions, TimeSpan)

使用指定的匹配选项和超时间隔在指定的输入字符串中搜索指定的正则表达式的所有匹配项。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Replace(String, MatchEvaluator)

在指定的输入字符串中,使用由 MatchEvaluator 委托返回的字符串替换与指定的正则表达式匹配的所有字符串。

Replace(String, MatchEvaluator, Int32)

在指定的输入字符串内,使用 MatchEvaluator 委托返回的字符串替换与某个正则表达式模式匹配的字符串(其数目为指定的最大数目)。

Replace(String, MatchEvaluator, Int32, Int32)

在指定的输入子字符串内,使用 MatchEvaluator 委托返回的字符串替换与某个正则表达式模式匹配的字符串(其数目为指定的最大数目)。

Replace(String, String)

在指定的输入字符串内,使用指定的替换字符串替换与某个正则表达式模式匹配的所有的字符串。

Replace(String, String, Int32)

在指定输入字符串内,使用指定替换字符串替换与某个正则表达式模式匹配的字符串(其数目为指定的最大数目)。

Replace(String, String, Int32, Int32)

在指定输入子字符串内,使用指定替换字符串替换与某个正则表达式模式匹配的字符串(其数目为指定的最大数目)。

Replace(String, String, MatchEvaluator)

在指定的输入字符串中,使用由 MatchEvaluator 委托返回的字符串替换与指定的正则表达式匹配的所有字符串。

Replace(String, String, MatchEvaluator, RegexOptions)

在指定的输入字符串中,使用由 MatchEvaluator 委托返回的字符串替换与指定的正则表达式匹配的所有字符串。 指定的选项将修改匹配操作。

Replace(String, String, MatchEvaluator, RegexOptions, TimeSpan)

在指定的输入字符串中,使用由 MatchEvaluator 委托返回的字符串替换与指定的正则表达式匹配的所有子字符串。 如果未找到匹配项,则其他参数指定修改匹配操作的选项和超时间隔。

Replace(String, String, String)

在指定的输入字符串内,使用指定的替换字符串替换与指定正则表达式匹配的所有字符串。

Replace(String, String, String, RegexOptions)

在指定的输入字符串内,使用指定的替换字符串替换与指定正则表达式匹配的所有字符串。 指定的选项将修改匹配操作。

Replace(String, String, String, RegexOptions, TimeSpan)

在指定的输入字符串内,使用指定的替换字符串替换与指定正则表达式匹配的所有字符串。 如果未找到匹配项,则其他参数指定修改匹配操作的选项和超时间隔。

Split(String)

在由 Regex 构造函数指定的正则表达式模式所定义的位置,将输入字符串拆分为子字符串数组。

Split(String, Int32)

在由 Regex 构造函数中指定的正则表达式定义的位置,将输入字符串拆分为子字符串数组指定的最大次数。

Split(String, Int32, Int32)

在由 Regex 构造函数中指定的正则表达式定义的位置,将输入字符串拆分为子字符串数组指定的最大次数。 从输入字符串的指定字符位置开始搜索正则表达式模式。

Split(String, String)

在由正则表达式模式定义的位置将输入字符串拆分为一个子字符串数组。

Split(String, String, RegexOptions)

在由指定正则表达式模式定义的位置将输入字符串拆分为一个子字符串数组。 指定的选项将修改匹配操作。

Split(String, String, RegexOptions, TimeSpan)

在由指定正则表达式模式定义的位置将输入字符串拆分为一个子字符串数组。 如果未找到匹配项,则其他参数指定修改匹配操作的选项和超时间隔。

ToString()

返回传入 Regex 构造函数的正则表达式模式。

Unescape(String)

转换输入字符串中的任何转义字符。

UseOptionC()
已过时。

CompileToAssembly 方法生成的 Regex 对象使用。

UseOptionR()
已过时。

CompileToAssembly 方法生成的 Regex 对象使用。

ValidateMatchTimeout(TimeSpan)

检查超时间隔是否在可接受的范围内。

显式接口实现

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

使用所需的数据填充 SerializationInfo 对象来反序列化当前 Regex 对象。

适用于

线程安全性

Regex 是不可变的, (只读) 和线程安全。 Regex 对象可以在任何线程上创建并在线程之间共享。 有关详细信息,请参阅 线程安全

另请参阅