.NET 正则表达式

正则表达式提供用于处理文本的强大、灵活且高效的方法。 正则表达式的广泛模式匹配表示法使您能够快速解析大量文本,以便:

  • 查找特定字符模式。
  • 验证文本以确保其匹配预定义的模式(如电子邮件地址)。
  • 提取、编辑、替换或删除文本子字符串。
  • 将提取的字符串添加到集合中以生成报表。

对于处理字符串或分析大量文本块的许多应用程序,正则表达式是不可或缺的工具。

正则表达式的工作原理

使用正则表达式处理文本的核心是正则表达式引擎,该引擎由 System.Text.RegularExpressions.Regex .NET 中的对象表示。 至少,使用正则表达式处理文本要求正则表达式引擎包含以下两项信息:

  • 要在文本中标识的正则表达式模式。

    在 .NET 中,正则表达式模式由特殊语法或语言定义,该语法或语言与 Perl 5 正则表达式兼容,并添加了一些附加功能,例如从右到左匹配。 有关详细信息,请参阅 正则表达式语言 - 快速参考

  • 要为正则表达式模式分析的文本。

Regex 的方法使你可以执行以下操作:

有关正则表达式对象模型的概述,请参阅 正则表达式对象模型

有关正则表达式语言的详细信息,请参阅 正则表达式语言 - 快速参考 或下载并打印以下小册子之一:

正则表达式示例

String 类包括字符串搜索和替换方法,当想要在较大的字符串中查找文本字符串时,可以使用这些方法。 如果要在较大的字符串中找到多个子字符串之一,或者想要标识字符串中的模式,如以下示例所示,正则表达式最有用。

警告

如果使用 System.Text.RegularExpressions 处理不受信任的输入,则传递一个超时。 恶意用户可以通过输入 RegularExpressions,从而导致 拒绝服务攻击。 使用 RegularExpressions 的 ASP.NET Core 框架 API 会传递一个超时。

小窍门

命名空间 System.Web.RegularExpressions 包含许多正则表达式对象,这些对象实现预定义的正则表达式模式,用于分析 HTML、XML 和 ASP.NET 文档中的字符串。 例如,类 TagRegex 标识字符串中的开始标记,类 CommentRegex 标识字符串中的 ASP.NET 注释。

示例 1:替换子字符串

假设邮件列表中包含一些姓名,其中有时包括头衔(例如 Mr.、Mrs.、Miss 或 Ms.)以及名字和姓氏。 假设在从列表中生成信封标签时不想包含标题。 在这种情况下,可以使用正则表达式删除标题,如以下示例所示:

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "(Mr\\.? |Mrs\\.? |Miss |Ms\\.? )";
      string[] names = { "Mr. Henry Hunt", "Ms. Sara Samuels",
                         "Abraham Adams", "Ms. Nicole Norris" };
      foreach (string name in names)
         Console.WriteLine(Regex.Replace(name, pattern, String.Empty));
   }
}
// The example displays the following output:
//    Henry Hunt
//    Sara Samuels
//    Abraham Adams
//    Nicole Norris
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim pattern As String = "(Mr\.? |Mrs\.? |Miss |Ms\.? )"
        Dim names() As String = {"Mr. Henry Hunt", "Ms. Sara Samuels", _
                                  "Abraham Adams", "Ms. Nicole Norris"}
        For Each name As String In names
            Console.WriteLine(Regex.Replace(name, pattern, String.Empty))
        Next
    End Sub
End Module
' The example displays the following output:
'    Henry Hunt
'    Sara Samuels
'    Abraham Adams
'    Nicole Norris

正则表达式模式 (Mr\.? |Mrs\.? |Miss |Ms\.? ) 匹配任何“Mr”、“Mr.”、“Mrs”、“Mrs.”、“Miss”、“Ms”或“Ms.”。 对方法的调用 Regex.Replace 将匹配的字符串替换为 String.Empty;换句话说,它将从原始字符串中删除它。

示例 2:标识重复字词

意外复制字词是编写器所犯的常见错误。 使用正则表达式标识重复的单词,如以下示例所示:

using System;
using System.Text.RegularExpressions;

public class Class1
{
   public static void Main()
   {
      string pattern = @"\b(\w+?)\s\1\b";
      string input = "This this is a nice day. What about this? This tastes good. I saw a a dog.";
      foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase))
         Console.WriteLine($"{match.Value} (duplicates '{match.Groups[1].Value}') at position {match.Index}");
   }
}
// The example displays the following output:
//       This this (duplicates 'This') at position 0
//       a a (duplicates 'a') at position 66
Imports System.Text.RegularExpressions

Module modMain
    Public Sub Main()
        Dim pattern As String = "\b(\w+?)\s\1\b"
        Dim input As String = "This this is a nice day. What about this? This tastes good. I saw a a dog."
        For Each match As Match In Regex.Matches(input, pattern, RegexOptions.IgnoreCase)
            Console.WriteLine("{0} (duplicates '{1}') at position {2}", _
                              match.Value, match.Groups(1).Value, match.Index)
        Next
    End Sub
End Module
' The example displays the following output:
'       This this (duplicates 'This') at position 0
'       a a (duplicates 'a') at position 66

正则表达式模式 \b(\w+?)\s\1\b 可以解释如下:

图案 解释
\b 在单词边界处开始。
(\w+?) 匹配一个或多个单词字符,但字符要尽可能的少。 它们共同组成一个可称为的 \1组。
\s 与空白字符匹配。
\1 匹配与命名为 \1 的组相等的子字符串。
\b 与字边界匹配。

调用 Regex.Matches 方法时,正则表达式选项设置为 RegexOptions.IgnoreCase. 因此,匹配操作是不区分大小写的,示例将子字符串“This this”识别为重复项。

输入字符串包括子字符串“this? This”。 但是,由于插入的标点符号,它未被识别为重复。

示例 3:动态生成文化敏感的正则表达式

下面的示例说明了正则表达式的强大功能与 .NET 全球化功能所提供的灵活性相结合。 它使用 NumberFormatInfo 对象来确定系统当前文化信息中的货币数值格式。 然后,它使用该信息动态构造一个正则表达式,该正则表达式从文本中提取货币值。 对于每个匹配项,它提取仅包含数字字符串的子组,将其转换为Decimal值,并计算累积总和。

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      // Define text to be parsed.
      string input = "Office expenses on 2/13/2008:\n" +
                     "Paper (500 sheets)                      $3.95\n" +
                     "Pencils (box of 10)                     $1.00\n" +
                     "Pens (box of 10)                        $4.49\n" +
                     "Erasers                                 $2.19\n" +
                     "Ink jet printer                        $69.95\n\n" +
                     "Total Expenses                        $ 81.58\n";

      // Get current culture's NumberFormatInfo object.
      NumberFormatInfo nfi = CultureInfo.CurrentCulture.NumberFormat;
      // Assign needed property values to variables.
      string currencySymbol = nfi.CurrencySymbol;
      bool symbolPrecedesIfPositive = nfi.CurrencyPositivePattern % 2 == 0;
      string groupSeparator = nfi.CurrencyGroupSeparator;
      string decimalSeparator = nfi.CurrencyDecimalSeparator;

      // Form regular expression pattern.
      string pattern = Regex.Escape( symbolPrecedesIfPositive ? currencySymbol : "") +
                       @"\s*[-+]?" + "([0-9]{0,3}(" + groupSeparator + "[0-9]{3})*(" +
                       Regex.Escape(decimalSeparator) + "[0-9]+)?)" +
                       (! symbolPrecedesIfPositive ? currencySymbol : "");
      Console.WriteLine( "The regular expression pattern is:");
      Console.WriteLine("   " + pattern);

      // Get text that matches regular expression pattern.
      MatchCollection matches = Regex.Matches(input, pattern,
                                              RegexOptions.IgnorePatternWhitespace);
      Console.WriteLine($"Found {matches.Count} matches.");

      // Get numeric string, convert it to a value, and add it to List object.
      List<decimal> expenses = new List<Decimal>();

      foreach (Match match in matches)
         expenses.Add(Decimal.Parse(match.Groups[1].Value));

      // Determine whether total is present and if present, whether it is correct.
      decimal total = 0;
      foreach (decimal value in expenses)
         total += value;

      if (total / 2 == expenses[expenses.Count - 1])
         Console.WriteLine($"The expenses total {expenses[expenses.Count - 1]:C2}.");
      else
         Console.WriteLine($"The expenses total {total:C2}.");
   }
}
// The example displays the following output:
//       The regular expression pattern is:
//          \$\s*[-+]?([0-9]{0,3}(,[0-9]{3})*(\.[0-9]+)?)
//       Found 6 matches.
//       The expenses total $81.58.
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Text.RegularExpressions

Public Module Example
    Public Sub Main()
        ' Define text to be parsed.
        Dim input As String = "Office expenses on 2/13/2008:" + vbCrLf + _
                              "Paper (500 sheets)                      $3.95" + vbCrLf + _
                              "Pencils (box of 10)                     $1.00" + vbCrLf + _
                              "Pens (box of 10)                        $4.49" + vbCrLf + _
                              "Erasers                                 $2.19" + vbCrLf + _
                              "Ink jet printer                        $69.95" + vbCrLf + vbCrLf + _
                              "Total Expenses                        $ 81.58" + vbCrLf
        ' Get current culture's NumberFormatInfo object.
        Dim nfi As NumberFormatInfo = CultureInfo.CurrentCulture.NumberFormat
        ' Assign needed property values to variables.
        Dim currencySymbol As String = nfi.CurrencySymbol
        Dim symbolPrecedesIfPositive As Boolean = CBool(nfi.CurrencyPositivePattern Mod 2 = 0)
        Dim groupSeparator As String = nfi.CurrencyGroupSeparator
        Dim decimalSeparator As String = nfi.CurrencyDecimalSeparator

        ' Form regular expression pattern.
        Dim pattern As String = Regex.Escape(CStr(IIf(symbolPrecedesIfPositive, currencySymbol, ""))) + _
                                "\s*[-+]?" + "([0-9]{0,3}(" + groupSeparator + "[0-9]{3})*(" + _
                                Regex.Escape(decimalSeparator) + "[0-9]+)?)" + _
                                CStr(IIf(Not symbolPrecedesIfPositive, currencySymbol, ""))
        Console.WriteLine("The regular expression pattern is: ")
        Console.WriteLine("   " + pattern)

        ' Get text that matches regular expression pattern.
        Dim matches As MatchCollection = Regex.Matches(input, pattern, RegexOptions.IgnorePatternWhitespace)
        Console.WriteLine("Found {0} matches. ", matches.Count)

        ' Get numeric string, convert it to a value, and add it to List object.
        Dim expenses As New List(Of Decimal)

        For Each match As Match In matches
            expenses.Add(Decimal.Parse(match.Groups.Item(1).Value))
        Next

        ' Determine whether total is present and if present, whether it is correct.
        Dim total As Decimal
        For Each value As Decimal In expenses
            total += value
        Next

        If total / 2 = expenses(expenses.Count - 1) Then
            Console.WriteLine("The expenses total {0:C2}.", expenses(expenses.Count - 1))
        Else
            Console.WriteLine("The expenses total {0:C2}.", total)
        End If
    End Sub
End Module
' The example displays the following output:
'       The regular expression pattern is:
'          \$\s*[-+]?([0-9]{0,3}(,[0-9]{3})*(\.[0-9]+)?)
'       Found 6 matches.
'       The expenses total $81.58.

在当前区域性为英语-美国(en-US)的计算机上,该示例动态生成正则表达式 \$\s*[-+]?([0-9]{0,3}(,[0-9]{3})*(\.[0-9]+)?)。 此正则表达式模式可以解释如下:

图案 解释
\$ 在输入字符串中查找美元符号 ($) 的一个匹配项。 正则表达式模式字符串包含一个反斜杠,用于表示美元符号要按字面解释,而不是作为正则表达式的锚点。 仅符号 $ 将指示正则表达式引擎应尝试在字符串末尾开始匹配。 为了确保当前区域性的货币符号不被错误解释为正则表达式符号,该示例调用 Regex.Escape 方法对该字符进行转义。
\s* 查找空白字符的零个或多个匹配项。
[-+]? 查找正号或负号的零个或一个匹配项。
([0-9]{0,3}(,[0-9]{3})*(\.[0-9]+)?) 外部括号将此表达式定义为捕获组或子表达式。 如果找到匹配项,可以从Group属性返回的GroupCollection对象中的第二个Match.Groups对象中检索到此部分匹配字符串的相关信息。 集合中的第一个元素表示整个匹配项。
[0-9]{0,3} 查找零到三个十进制数字 0 到 9 的出现次数。
(,[0-9]{3})* 查找后跟三个十进制数字的组分隔符的零个或多个匹配项。
\. 查找小数分隔符的一个匹配项。
[0-9]+ 查找一个或多个十进制数字。
(\.[0-9]+)? 查找后跟至少一个十进制数字的小数分隔符的零个或一个匹配项。

如果在输入字符串中找到每个子模式,则匹配成功,并且 Match 包含有关匹配的信息的对象将添加到 MatchCollection 该对象。

标题 DESCRIPTION
正则表达式语言 - 快速参考 提供有关可用于定义正则表达式的字符、运算符和构造集的信息。
正则表达式对象模型 提供说明如何使用正则表达式类的信息和代码示例。
正则表达式行为的详细信息 提供有关 .NET 正则表达式的功能和行为的信息。
在 Visual Studio 中使用正则表达式

参考文献