正则表达式对象模型The Regular Expression Object Model

本主题介绍了处理 .NET 正则表达式时使用的对象模型。This topic describes the object model used in working with .NET regular expressions. 它包含下列部分:It contains the following sections:

正则表达式引擎The Regular Expression Engine

.NET 中的正则表达式引擎由 Regex 类表示。The regular expression engine in .NET is represented by the Regex class. 正则表达式引擎负责分析和编译正则表达式,并执行用于将正则表达式模式与输入字符串相匹配的操作。The regular expression engine is responsible for parsing and compiling a regular expression, and for performing operations that match the regular expression pattern with an input string. 此引擎是 .NET 正则表达式对象模型中的主要组件。The engine is the central component in the .NET regular expression object model.

可以通过以下两种方式之一使用正则表达式引擎:You can use the regular expression engine in either of two ways:

  • 通过调用 Regex 类的静态方法。By calling the static methods of the Regex class. 方法参数包含输入字符串和正则表达式模式。The method parameters include the input string and the regular expression pattern. 正则表达式引擎会缓存静态方法调用中使用的正则表达式,这样一来,重复调用使用同一正则表达式的静态正则表达式方法将提供相对良好的性能。The regular expression engine caches regular expressions that are used in static method calls, so repeated calls to static regular expression methods that use the same regular expression offer relatively good performance.

  • 通过实例化 Regex 对象,采用的方式是将一个正则表达式传递给类构造函数。By instantiating a Regex object, by passing a regular expression to the class constructor. 在此情况下,Regex 对象是不可变的(只读),它表示一个与单个正则表达式紧密耦合的正则表达式引擎。In this case, the Regex object is immutable (read-only) and represents a regular expression engine that is tightly coupled with a single regular expression. 由于未对 Regex 实例使用的正则表达式进行缓存,因此不应使用同一正则表达式实例化 Regex 对象多次。Because regular expressions used by Regex instances are not cached, you should not instantiate a Regex object multiple times with the same regular expression.

可以调用 Regex 类的方法来执行下列操作:You can call the methods of the Regex class to perform the following operations:

  • 确定字符串是否与正则表达式模式匹配。Determine whether a string matches a regular expression pattern.

  • 提取单个匹配项或第一个匹配项。Extract a single match or the first match.

  • 提取所有匹配项。Extract all matches.

  • 替换匹配的子字符串。Replace a matched substring.

  • 将单个字符串拆分成一个字符串数组。Split a single string into an array of strings.

以下各部分对这些操作进行了描述。These operations are described in the following sections.

匹配正则表达式模式Matching a Regular Expression Pattern

如果字符串与此模式匹配,则 Regex.IsMatch 方法返回 true;如果字符串与此模式不匹配,则该方法返回 falseThe Regex.IsMatch method returns true if the string matches the pattern, or false if it does not. IsMatch 方法通常用于验证字符串输入。The IsMatch method is often used to validate string input. 例如,下面的代码将确保字符串与有效的美国社会保障号匹配。For example, the following code ensures that a string matches a valid social security number in the United States.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string[] values = { "111-22-3333", "111-2-3333"};
      string pattern = @"^\d{3}-\d{2}-\d{4}$";
      foreach (string value in values) {
         if (Regex.IsMatch(value, pattern))
            Console.WriteLine("{0} is a valid SSN.", value);
         else
            Console.WriteLine("{0}: Invalid", value);
      }
   }
}
// The example displays the following output:
//       111-22-3333 is a valid SSN.
//       111-2-3333: Invalid
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim values() As String = {"111-22-3333", "111-2-3333"}
        Dim pattern As String = "^\d{3}-\d{2}-\d{4}$"
        For Each value As String In values
            If Regex.IsMatch(value, pattern) Then
                Console.WriteLine("{0} is a valid SSN.", value)
            Else
                Console.WriteLine("{0}: Invalid", value)
            End If
        Next
    End Sub
End Module
' The example displays the following output:
'       111-22-3333 is a valid SSN.
'       111-2-3333: Invalid

正则表达式模式 ^\d{3}-\d{2}-\d{4}$ 的含义如下表所示。The regular expression pattern ^\d{3}-\d{2}-\d{4}$ is interpreted as shown in the following table.

模式Pattern 描述Description
^ 匹配输入字符串的开头部分。Match the beginning of the input string.
\d{3} 匹配三个十进制数字。Match three decimal digits.
- 匹配连字符。Match a hyphen.
\d{2} 匹配两个十进制数字。Match two decimal digits.
- 匹配连字符。Match a hyphen.
\d{4} 匹配四个十进制数字。Match four decimal digits.
$ 匹配输入字符串的末尾部分。Match the end of the input string.

提取单个匹配项或第一个匹配项Extracting a Single Match or the First Match

Regex.Match 方法返回一个 Match 对象,该对象包含有关与正则表达式模式匹配的第一个子字符串的信息。The Regex.Match method returns a Match object that contains information about the first substring that matches a regular expression pattern. 如果 Match.Success 属性返回 true,则表示已找到一个匹配项,可以通过调用 Match.NextMatch 方法来检索有关后续匹配项的信息。If the Match.Success property returns true, indicating that a match was found, you can retrieve information about subsequent matches by calling the Match.NextMatch method. 这些方法调用可以继续进行,直到 Match.Success 属性返回 falseThese method calls can continue until the Match.Success property returns false. 例如,下面的代码使用 Regex.Match(String, String) 方法查找重复的单词在字符串中的第一个匹配项。For example, the following code uses the Regex.Match(String, String) method to find the first occurrence of a duplicated word in a string. 然后,此代码调用 Match.NextMatch 方法查找任何其他匹配项。It then calls the Match.NextMatch method to find any additional occurrences. 该示例将在每次调用方法后检查 Match.Success 属性以确定当前匹配是否成功,并确定是否应接着调用 Match.NextMatch 方法。The example examines the Match.Success property after each method call to determine whether the current match was successful and whether a call to the Match.NextMatch method should follow.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "This is a a farm that that raises dairy cattle.";
      string pattern = @"\b(\w+)\W+(\1)\b";
      Match match = Regex.Match(input, pattern);
      while (match.Success)
      {
         Console.WriteLine("Duplicate '{0}' found at position {1}.",
                           match.Groups[1].Value, match.Groups[2].Index);
         match = match.NextMatch();
      }
   }
}
// The example displays the following output:
//       Duplicate 'a' found at position 10.
//       Duplicate 'that' found at position 22.
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim input As String = "This is a a farm that that raises dairy cattle."
        Dim pattern As String = "\b(\w+)\W+(\1)\b"
        Dim match As Match = Regex.Match(input, pattern)
        Do While match.Success
            Console.WriteLine("Duplicate '{0}' found at position {1}.", _
                              match.Groups(1).Value, match.Groups(2).Index)
            match = match.NextMatch()
        Loop
    End Sub
End Module
' The example displays the following output:
'       Duplicate 'a' found at position 10.
'       Duplicate 'that' found at position 22.

正则表达式模式 \b(\w+)\W+(\1)\b 的含义如下表所示。The regular expression pattern \b(\w+)\W+(\1)\b is interpreted as shown in the following table.

模式Pattern 描述Description
\b 在单词边界处开始匹配。Begin the match on a word boundary.
(\w+) 匹配一个或多个单词字符。Match one or more word characters. 这是第一个捕获组。This is the first capturing group.
\W+ 匹配一个或多个非单词字符。Match one or more non-word characters.
(\1) 与第一个捕获的字符串匹配。Match the first captured string. 这是第二个捕获组。This is the second capturing group.
\b 在单词边界处结束匹配。End the match on a word boundary.

提取所有匹配项Extracting All Matches

Regex.Matches 方法返回一个 MatchCollection 对象,该对象包含有关正则表达式引擎在输入字符串中找到的所有匹配项的信息。The Regex.Matches method returns a MatchCollection object that contains information about all matches that the regular expression engine found in the input string. 例如,可重写上一示例以调用 Matches 方法,而不是调用 MatchNextMatch 方法。For example, the previous example could be rewritten to call the Matches method instead of the Match and NextMatch methods.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "This is a a farm that that raises dairy cattle.";
      string pattern = @"\b(\w+)\W+(\1)\b";
      foreach (Match match in Regex.Matches(input, pattern))
         Console.WriteLine("Duplicate '{0}' found at position {1}.",
                           match.Groups[1].Value, match.Groups[2].Index);
   }
}
// The example displays the following output:
//       Duplicate 'a' found at position 10.
//       Duplicate 'that' found at position 22.
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim input As String = "This is a a farm that that raises dairy cattle."
        Dim pattern As String = "\b(\w+)\W+(\1)\b"
        For Each match As Match In Regex.Matches(input, pattern)
            Console.WriteLine("Duplicate '{0}' found at position {1}.", _
                              match.Groups(1).Value, match.Groups(2).Index)
        Next
    End Sub
End Module
' The example displays the following output:
'       Duplicate 'a' found at position 10.
'       Duplicate 'that' found at position 22.

替换匹配的子字符串Replacing a Matched Substring

Regex.Replace 方法会将与正则表达式模式匹配的每个子字符串替换为指定的字符串或正则表达式模式,并返回进行了替换的整个输入字符串。The Regex.Replace method replaces each substring that matches the regular expression pattern with a specified string or regular expression pattern, and returns the entire input string with replacements. 例如,下面的代码在字符串的十进制数字前添加了美国货币符号。For example, the following code adds a U.S. currency symbol before a decimal number in a string.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b\d+\.\d{2}\b";
      string replacement = "$$$&";
      string input = "Total Cost: 103.64";
      Console.WriteLine(Regex.Replace(input, pattern, replacement));
   }
}
// The example displays the following output:
//       Total Cost: $103.64
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim pattern As String = "\b\d+\.\d{2}\b"
        Dim replacement As String = "$$$&"
        Dim input As String = "Total Cost: 103.64"
        Console.WriteLine(Regex.Replace(input, pattern, replacement))
    End Sub
End Module
' The example displays the following output:
'       Total Cost: $103.64

正则表达式模式 \b\d+\.\d{2}\b 的含义如下表所示。The regular expression pattern \b\d+\.\d{2}\b is interpreted as shown in the following table.

模式Pattern 描述Description
\b 在单词边界处开始匹配。Begin the match at a word boundary.
\d+ 匹配一个或多个十进制数字。Match one or more decimal digits.
\. 匹配句点。Match a period.
\d{2} 匹配两个十进制数字。Match two decimal digits.
\b 在单词边界处结束匹配。End the match at a word boundary.

替换模式 $$$& 的含义如下表所示。The replacement pattern $$$& is interpreted as shown in the following table.

模式Pattern 替换字符串Replacement string
$$ 美元符号 ($) 字符。The dollar sign ($) character.
$& 整个匹配的子字符串。The entire matched substring.

将单个字符串拆分成一个字符串数组Splitting a Single String into an Array of Strings

Regex.Split 方法在由正则表达式匹配项定义的位置拆分输入字符串。The Regex.Split method splits the input string at the positions defined by a regular expression match. 例如,下面的代码将编号列表中的项置于字符串数组中。For example, the following code places the items in a numbered list into a string array.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "1. Eggs 2. Bread 3. Milk 4. Coffee 5. Tea";
      string pattern = @"\b\d{1,2}\.\s";
      foreach (string item in Regex.Split(input, pattern))
      {
         if (! String.IsNullOrEmpty(item))
            Console.WriteLine(item);
      }
   }
}
// The example displays the following output:
//       Eggs
//       Bread
//       Milk
//       Coffee
//       Tea
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim input As String = "1. Eggs 2. Bread 3. Milk 4. Coffee 5. Tea"
        Dim pattern As String = "\b\d{1,2}\.\s"
        For Each item As String In Regex.Split(input, pattern)
            If Not String.IsNullOrEmpty(item) Then
                Console.WriteLine(item)
            End If
        Next
    End Sub
End Module
' The example displays the following output:
'       Eggs
'       Bread
'       Milk
'       Coffee
'       Tea

正则表达式模式 \b\d{1,2}\.\s 的含义如下表所示。The regular expression pattern \b\d{1,2}\.\s is interpreted as shown in the following table.

模式Pattern 描述Description
\b 在单词边界处开始匹配。Begin the match at a word boundary.
\d{1,2} 匹配一个或两个十进制数字。Match one or two decimal digits.
\. 匹配句点。Match a period.
\s 与空白字符匹配。Match a white-space character.

MatchCollection 和 Match 对象The MatchCollection and Match Objects

Regex 方法返回作为正则表达式对象模型的一部分的两个对象:MatchCollection 对象和 Match 对象。Regex methods return two objects that are part of the regular expression object model: the MatchCollection object, and the Match object.

Match 集合The Match Collection

Regex.Matches 方法返回一个 MatchCollection 对象,该对象包含多个 Match 对象,这些对象表示正则表达式引擎在输入字符串中找到的所有匹配项(其顺序为这些匹配项在输入字符串中的显示顺序)。The Regex.Matches method returns a MatchCollection object that contains Match objects that represent all the matches that the regular expression engine found, in the order in which they occur in the input string. 如果没有匹配项,则此方法将返回一个不包含任何成员的 MatchCollection 对象。If there are no matches, the method returns a MatchCollection object with no members. 利用 MatchCollection.Item[] 属性,你可以按照索引(从零到将 MatchCollection.Count 属性的值减 1 所得的值)访问集合中的各个成员。The MatchCollection.Item[] property lets you access individual members of the collection by index, from zero to one less than the value of the MatchCollection.Count property. Item[] 是集合的索引器(在 C# 中)和默认属性(在 Visual Basic 中)。Item[] is the collection's indexer (in C#) and default property (in Visual Basic).

默认情况下,调用 Regex.Matches 方法会使用延迟计算来填充 MatchCollection 对象。By default, the call to the Regex.Matches method uses lazy evaluation to populate the MatchCollection object. 访问需要完全填充的集合的属性(如 MatchCollection.CountMatchCollection.Item[] 属性)可能会降低性能。Access to properties that require a fully populated collection, such as the MatchCollection.Count and MatchCollection.Item[] properties, may involve a performance penalty. 因此,建议你使用由 IEnumerator 方法返回的 MatchCollection.GetEnumerator 对象访问该集合。As a result, we recommend that you access the collection by using the IEnumerator object that is returned by the MatchCollection.GetEnumerator method. 各种语言都提供了用于包装集合的 IEnumerator 接口的构造(如 Visual Basic 中的 For Each 和 C# 中的 foreach)。Individual languages provide constructs, such as For Each in Visual Basic and foreach in C#, that wrap the collection's IEnumerator interface.

下面的示例使用 Regex.Matches(String) 方法将在输入字符串中找到的所有匹配项填充到 MatchCollection 对象中。The following example uses the Regex.Matches(String) method to populate a MatchCollection object with all the matches found in an input string. 此示例枚举了该集合,将匹配项复制到字符串数组并将字符位置记录在整数数组中。The example enumerates the collection, copies the matches to a string array, and records the character positions in an integer array.

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

public class Example
{
   public static void Main()
   {
       MatchCollection matches;
       List<string> results = new List<string>();
       List<int> matchposition = new List<int>();

       // Create a new Regex object and define the regular expression.
       Regex r = new Regex("abc");
       // Use the Matches method to find all matches in the input string.
       matches = r.Matches("123abc4abcd");
       // Enumerate the collection to retrieve all matches and positions.
       foreach (Match match in matches)
       {
          // Add the match string to the string array.
           results.Add(match.Value);
           // Record the character position where the match was found.
           matchposition.Add(match.Index);
       }
       // List the results.
       for (int ctr = 0; ctr < results.Count; ctr++)
         Console.WriteLine("'{0}' found at position {1}.",
                           results[ctr], matchposition[ctr]);
   }
}
// The example displays the following output:
//       'abc' found at position 3.
//       'abc' found at position 7.
Imports System.Collections.Generic
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim matches As MatchCollection
        Dim results As New List(Of String)
        Dim matchposition As New List(Of Integer)

        ' Create a new Regex object and define the regular expression.
        Dim r As New Regex("abc")
        ' Use the Matches method to find all matches in the input string.
        matches = r.Matches("123abc4abcd")
        ' Enumerate the collection to retrieve all matches and positions.
        For Each match As Match In matches
            ' Add the match string to the string array.
            results.Add(match.Value)
            ' Record the character position where the match was found.
            matchposition.Add(match.Index)
        Next
        ' List the results.
        For ctr As Integer = 0 To results.Count - 1
            Console.WriteLine("'{0}' found at position {1}.", _
                              results(ctr), matchposition(ctr))
        Next
    End Sub
End Module
' The example displays the following output:
'       'abc' found at position 3.
'       'abc' found at position 7.

Match 类The Match

Match 类表示单个正则表达式匹配项的结果。The Match class represents the result of a single regular expression match. 可以通过两种方式访问 Match 对象:You can access Match objects in two ways:

  • 通过从 MatchCollection 方法返回的 Regex.Matches 对象检索这些对象。By retrieving them from the MatchCollection object that is returned by the Regex.Matches method. 若要检索各个 Match 对象,请通过使用 foreach(在 C# 中)或 For Each...Next(在 Visual Basic 中)构造循环访问集合;或者使用 MatchCollection.Item[] 属性以按索引或名称检索特定的 Match 对象。To retrieve individual Match objects, iterate the collection by using a foreach (in C#) or For Each...Next (in Visual Basic) construct, or use the MatchCollection.Item[] property to retrieve a specific Match object either by index or by name. 也可以通过按索引(从零到将集合中的对象数减去 1 所得的值)循环访问集合来检索集合中的各个 Match 对象。You can also retrieve individual Match objects from the collection by iterating the collection by index, from zero to one less that the number of objects in the collection. 但是,此方法不使用延迟计算,因为它将访问 MatchCollection.Count 属性。However, this method does not take advantage of lazy evaluation, because it accesses the MatchCollection.Count property.

    下面的示例通过使用 MatchMatchCollection...foreach 构造循环访问集合,来从 For Each 对象中检索各个 Next 对象。The following example retrieves individual Match objects from a MatchCollection object by iterating the collection using the foreach or For Each...Next construct. 正则表达式只是与输入字符串中的字符串“abc”匹配。The regular expression simply matches the string "abc" in the input string.

    using System;
    using System.Text.RegularExpressions;
    
    public class Example
    {
       public static void Main()
       {
          string pattern = "abc";
          string input = "abc123abc456abc789";
          foreach (Match match in Regex.Matches(input, pattern))
             Console.WriteLine("{0} found at position {1}.",
                               match.Value, match.Index);
       }
    }
    // The example displays the following output:
    //       abc found at position 0.
    //       abc found at position 6.
    //       abc found at position 12.
    
    Imports System.Text.RegularExpressions
    
    Module Example
        Public Sub Main()
            Dim pattern As String = "abc"
            Dim input As String = "abc123abc456abc789"
            For Each match As Match In Regex.Matches(input, pattern)
                Console.WriteLine("{0} found at position {1}.", _
                                  match.Value, match.Index)
            Next
        End Sub
    End Module
    ' The example displays the following output:
    '       abc found at position 0.
    '       abc found at position 6.
    '       abc found at position 12.
    
  • 通过调用 Regex.Match 方法,此方法返回一个 Match 对象,该对象表示字符串中的第一个匹配项或字符串的一部分。By calling the Regex.Match method, which returns a Match object that represents the first match in a string or a portion of a string. 可以通过检索 Match.Success 属性的值确定是否已找到匹配项。You can determine whether the match has been found by retrieving the value of the Match.Success property. 若要检索表示后续匹配项的 Match 对象,请重复调用 Match.NextMatch 方法,直到返回的 Success 对象的 Match 属性为 falseTo retrieve Match objects that represent subsequent matches, call the Match.NextMatch method repeatedly, until the Success property of the returned Match object is false.

    下面的示例使用 Regex.Match(String, String)Match.NextMatch 方法来匹配输入字符串中的字符串“abc”。The following example uses the Regex.Match(String, String) and Match.NextMatch methods to match the string "abc" in the input string.

    using System;
    using System.Text.RegularExpressions;
    
    public class Example
    {
       public static void Main()
       {
          string pattern = "abc";
          string input = "abc123abc456abc789";
          Match match = Regex.Match(input, pattern);
          while (match.Success)
          {
             Console.WriteLine("{0} found at position {1}.",
                               match.Value, match.Index);
             match = match.NextMatch();
          }
       }
    }
    // The example displays the following output:
    //       abc found at position 0.
    //       abc found at position 6.
    //       abc found at position 12.
    
    Imports System.Text.RegularExpressions
    
    Module Example
        Public Sub Main()
            Dim pattern As String = "abc"
            Dim input As String = "abc123abc456abc789"
            Dim match As Match = Regex.Match(input, pattern)
            Do While match.Success
                Console.WriteLine("{0} found at position {1}.", _
                                  match.Value, match.Index)
                match = match.NextMatch()
            Loop
        End Sub
    End Module
    ' The example displays the following output:
    '       abc found at position 0.
    '       abc found at position 6.
    '       abc found at position 12.
    

Match 类的以下两个属性都将返回集合对象:Two properties of the Match class return collection objects:

  • Match.Groups 属性返回一个 GroupCollection 对象,该对象包含有关与正则表达式模式中的捕获组匹配的子字符串的信息。The Match.Groups property returns a GroupCollection object that contains information about the substrings that match capturing groups in the regular expression pattern.

  • Match.Captures 属性返回一个 CaptureCollection 对象,该对象的使用是有限制的。The Match.Captures property returns a CaptureCollection object that is of limited use. 不会为其 Success 属性为 falseMatch 的对象填充集合。The collection is not populated for a Match object whose Success property is false. 否则,它将包含一个 Capture 对象,该对象具有的信息与 Match 对象具有的信息相同。Otherwise, it contains a single Capture object that has the same information as the Match object.

有关这些对象的更多信息,请参阅本主题后面的组集合捕获集合部分。For more information about these objects, see The Group Collection and The Capture Collection sections later in this topic.

Match 类的另外两个属性提供了有关匹配项的信息。Two additional properties of the Match class provide information about the match. Match.Value 属性返回输入字符串中与正则表达式模式匹配的子字符串。The Match.Value property returns the substring in the input string that matches the regular expression pattern. Match.Index 属性返回输入字符串中匹配的字符串的起始位置(从零开始)。The Match.Index property returns the zero-based starting position of the matched string in the input string.

Match 类还具有两个模式匹配方法:The Match class also has two pattern-matching methods:

  • Match.NextMatch 方法查找位于由当前的 Match 对象表示的匹配项之后的匹配项,并返回表示该匹配项的 Match 对象。The Match.NextMatch method finds the match after the match represented by the current Match object, and returns a Match object that represents that match.

  • Match.Result 方法对匹配的字符串执行指定的替换操作并返回相应结果。The Match.Result method performs a specified replacement operation on the matched string and returns the result.

下面的示例使用 Match.Result 方法在每个包含两个小数位的数字前预置一个 $ 符号和一个空格。The following example uses the Match.Result method to prepend a $ symbol and a space before every number that includes two fractional digits.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b\d+(,\d{3})*\.\d{2}\b";
      string input = "16.32\n194.03\n1,903,672.08";

      foreach (Match match in Regex.Matches(input, pattern))
         Console.WriteLine(match.Result("$$ $&"));
   }
}
// The example displays the following output:
//       $ 16.32
//       $ 194.03
//       $ 1,903,672.08
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim pattern As String = "\b\d+(,\d{3})*\.\d{2}\b"
        Dim input As String = "16.32" + vbCrLf + "194.03" + vbCrLf + "1,903,672.08"

        For Each match As Match In Regex.Matches(input, pattern)
            Console.WriteLine(match.Result("$$ $&"))
        Next
    End Sub
End Module
' The example displays the following output:
'       $ 16.32
'       $ 194.03
'       $ 1,903,672.08

正则表达式模式 \b\d+(,\d{3})*\.\d{2}\b 的定义如下表所示。The regular expression pattern \b\d+(,\d{3})*\.\d{2}\b is defined as shown in the following table.

模式Pattern 描述Description
\b 在单词边界处开始匹配。Begin the match at a word boundary.
\d+ 匹配一个或多个十进制数字。Match one or more decimal digits.
(,\d{3})* 匹配零个或多个以下模式:一个逗号后跟三个十进制数字。Match zero or more occurrences of a comma followed by three decimal digits.
\. 匹配小数点字符。Match the decimal point character.
\d{2} 匹配两个十进制数字。Match two decimal digits.
\b 在单词边界处结束匹配。End the match at a word boundary.

替换模式 $$ $& 指示匹配的子字符串应由美元符号 ($)($$ 模式)、空格和匹配项的值($& 模式)替换。The replacement pattern $$ $& indicates that the matched substring should be replaced by a dollar sign ($) symbol (the $$ pattern), a space, and the value of the match (the $& pattern).

返回页首Back to top

组集合The Group Collection

Match.Groups 属性返回一个 GroupCollection 对象,该对象包含多个 Group 对象,这些对象表示单个匹配项中的捕获的组。The Match.Groups property returns a GroupCollection object that contains Group objects that represent captured groups in a single match. 集合中的第一个 Group 对象(位于索引 0 处)表示整个匹配项。The first Group object in the collection (at index 0) represents the entire match. 此对象后面的每个对象均表示一个捕获组的结果。Each object that follows represents the results of a single capturing group.

可以使用 Group 属性检索集合中的各个 GroupCollection.Item[] 对象。You can retrieve individual Group objects in the collection by using the GroupCollection.Item[] property. 可以在集合中按未命名组的序号位置来检索未命名组,也可以按命名组的名称或序号位置来检索命名组。You can retrieve unnamed groups by their ordinal position in the collection, and retrieve named groups either by name or by ordinal position. 未命名捕获将首先在集合中显示,并将按照未命名捕获在正则表达式模式中出现的顺序从左至右对它们进行索引。Unnamed captures appear first in the collection, and are indexed from left to right in the order in which they appear in the regular expression pattern. 在对未命名捕获进行索引后,将按照命名捕获在正则表达式模式中出现的顺序从左至右对它们进行索引。Named captures are indexed after unnamed captures, from left to right in the order in which they appear in the regular expression pattern. 若要确定在特定的正则表达式匹配方法返回的集合中哪些编号的组可用,可以调用实例 Regex.GetGroupNumbers 方法。To determine what numbered groups are available in the collection returned for a particular regular expression matching method, you can call the instance Regex.GetGroupNumbers method. 若要确定集合中哪些命名的组可用,可以调用实例 Regex.GetGroupNames 方法。To determine what named groups are available in the collection, you can call the instance Regex.GetGroupNames method. 这两种方法在分析通过任何正则表达式找到的匹配的常规用途例程中都特别有用。Both methods are particularly useful in general-purpose routines that analyze the matches found by any regular expression.

GroupCollection.Item[] 属性是集合的索引器(在 C# 中)和集合对象的默认属性(在 Visual Basic 中)。The GroupCollection.Item[] property is the indexer of the collection in C# and the collection object's default property in Visual Basic. 这表示可以按索引(对于命名组,可以按名称)访问各个 Group 对象,如下所示:This means that individual Group objects can be accessed by index (or by name, in the case of named groups) as follows:

Group group = match.Groups[ctr];
Dim group As Group = match.Groups(ctr)

下面的示例定义一个正则表达式,该表达式使用分组构造捕获日期的年、月和日部分。The following example defines a regular expression that uses grouping constructs to capture the month, day, and year of a date.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b(\w+)\s(\d{1,2}),\s(\d{4})\b";
      string input = "Born: July 28, 1989";
      Match match = Regex.Match(input, pattern);
      if (match.Success)
         for (int ctr = 0; ctr <  match.Groups.Count; ctr++)
            Console.WriteLine("Group {0}: {1}", ctr, match.Groups[ctr].Value);
    }
}
// The example displays the following output:
//       Group 0: July 28, 1989
//       Group 1: July
//       Group 2: 28
//       Group 3: 1989
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim pattern As String = "\b(\w+)\s(\d{1,2}),\s(\d{4})\b"
        Dim input As String = "Born: July 28, 1989"
        Dim match As Match = Regex.Match(input, pattern)
        If match.Success Then
            For ctr As Integer = 0 To match.Groups.Count - 1
                Console.WriteLine("Group {0}: {1}", ctr, match.Groups(ctr).Value)
            Next
        End If
    End Sub
End Module
' The example displays the following output:
'       Group 0: July 28, 1989
'       Group 1: July
'       Group 2: 28
'       Group 3: 1989

正则表达式模式 \b(\w+)\s(\d{1,2}),\s(\d{4})\b 的定义如下表所示。The regular expression pattern \b(\w+)\s(\d{1,2}),\s(\d{4})\b is defined as shown in the following table.

模式Pattern 描述Description
\b 在单词边界处开始匹配。Begin the match at a word boundary.
(\w+) 匹配一个或多个单词字符。Match one or more word characters. 这是第一个捕获组。This is the first capturing group.
\s 与空白字符匹配。Match a white-space character.
(\d{1,2}) 匹配一个或两个十进制数字。Match one or two decimal digits. 这是第二个捕获组。This is the second capturing group.
, 匹配逗号。Match a comma.
\s 与空白字符匹配。Match a white-space character.
(\d{4}) 匹配四个十进制数字。Match four decimal digits. 这是第三个捕获组。This is the third capturing group.
\b 在单词边界处结束匹配。End the match on a word boundary.

返回页首Back to top

捕获的组The Captured Group

Group 类表示来自单个捕获组的结果。The Group class represents the result from a single capturing group. 表示正则表达式中定义的捕获组的组对象由 Item[] 属性所返回的 GroupCollection 对象的 Match.Groups 属性返回。Group objects that represent the capturing groups defined in a regular expression are returned by the Item[] property of the GroupCollection object returned by the Match.Groups property. Item[] 属性是索引器(在 C# 中)和 Group 类的默认属性(在 Visual Basic 中)。The Item[] property is the indexer (in C#) and the default property (in Visual Basic) of the Group class. 也可以使用 foreachFor Each 构造循环访问集合,从而检索各个成员。You can also retrieve individual members by iterating the collection using the foreach or For Each construct. 有关示例,请参见上一部分。For an example, see the previous section.

下面的示例使用嵌套的分组构造来将子字符串捕获到组中。The following example uses nested grouping constructs to capture substrings into groups. 正则表达式模式 (a(b))c 将匹配字符串“abc”。The regular expression pattern (a(b))c matches the string "abc". 它会将子字符串“ab”分配给第一个捕获组,并将子字符串“b”分配给第二个捕获组。It assigns the substring "ab" to the first capturing group, and the substring "b" to the second capturing group.

var matchposition = new List<int>();
var results = new List<string>();
// Define substrings abc, ab, b.
var r = new Regex("(a(b))c");
Match m = r.Match("abdabc");
for (int i = 0; m.Groups[i].Value != ""; i++)
{
    // Add groups to string array.
    results.Add(m.Groups[i].Value);
    // Record character position.
    matchposition.Add(m.Groups[i].Index);
}

// Display the capture groups.
for (int ctr = 0; ctr < results.Count; ctr++)
    Console.WriteLine("{0} at position {1}",
                      results[ctr], matchposition[ctr]);
// The example displays the following output:
//       abc at position 3
//       ab at position 3
//       b at position 4
Dim matchposition As New List(Of Integer)
Dim results As New List(Of String)
' Define substrings abc, ab, b.
Dim r As New Regex("(a(b))c")
Dim m As Match = r.Match("abdabc")
Dim i As Integer = 0
While Not (m.Groups(i).Value = "")
    ' Add groups to string array.
    results.Add(m.Groups(i).Value)
    ' Record character position. 
    matchposition.Add(m.Groups(i).Index)
    i += 1
End While

' Display the capture groups.
For ctr As Integer = 0 to results.Count - 1
    Console.WriteLine("{0} at position {1}", _
                      results(ctr), matchposition(ctr))
Next
' The example displays the following output:
'       abc at position 3
'       ab at position 3
'       b at position 4

下面的示例使用命名的分组构造,从包含“DATANAME:VALUE”格式的数据的字符串中捕获子字符串,正则表达式通过冒号 (:) 拆分数据。The following example uses named grouping constructs to capture substrings from a string that contains data in the format "DATANAME:VALUE", which the regular expression splits at the colon (:).

var r = new Regex(@"^(?<name>\w+):(?<value>\w+)");
Match m = r.Match("Section1:119900");
Console.WriteLine(m.Groups["name"].Value);
Console.WriteLine(m.Groups["value"].Value);
// The example displays the following output:
//       Section1
//       119900
Dim r As New Regex("^(?<name>\w+):(?<value>\w+)")
Dim m As Match = r.Match("Section1:119900")
Console.WriteLine(m.Groups("name").Value)
Console.WriteLine(m.Groups("value").Value)
' The example displays the following output:
'       Section1
'       119900

正则表达式模式 ^(?<name>\w+):(?<value>\w+) 的定义如下表所示。The regular expression pattern ^(?<name>\w+):(?<value>\w+) is defined as shown in the following table.

模式Pattern 描述Description
^ 从输入字符串的开头部分开始匹配。Begin the match at the beginning of the input string.
(?<name>\w+) 匹配一个或多个单词字符。Match one or more word characters. 此捕获组的名称为 nameThe name of this capturing group is name.
: 匹配冒号。Match a colon.
(?<value>\w+) 匹配一个或多个单词字符。Match one or more word characters. 此捕获组的名称为 valueThe name of this capturing group is value.

Group 类的属性提供有关捕获组的信息:Group.Value 属性包含捕获子字符串,Group.Index 属性在输入文本中指示捕获组的起始位置,Group.Length 属性包含捕获文本的长度,Group.Success 属性指示子字符串是否与捕获组所定义的模式匹配。The properties of the Group class provide information about the captured group: The Group.Value property contains the captured substring, the Group.Index property indicates the starting position of the captured group in the input text, the Group.Length property contains the length of the captured text, and the Group.Success property indicates whether a substring matched the pattern defined by the capturing group.

通过对组应用量符(有关详细信息,请参阅量符),可以每捕获组修改一个捕获的关系,具体方式分为以下两种:Applying quantifiers to a group (for more information, see Quantifiers) modifies the relationship of one capture per capturing group in two ways:

  • 如果对组应用 **? 限定符(将指定零个或多个匹配项),则捕获组在输入字符串中可能没有匹配项。If the * or *? quantifier (which specifies zero or more matches) is applied to a group, a capturing group may not have a match in the input string. 在没有捕获的文本时,将如下表所示设置 Group 对象的属性。When there is no captured text, the properties of the Group object are set as shown in the following table.

    组属性Group property “值”Value
    Success false
    Value String.Empty
    Length 00

    下面的示例进行了这方面的演示。The following example provides an illustration. 在正则表达式模式 aaa(bbb)*ccc 中,可以匹配第一个捕获组(子字符串“bbb”)零次或多次。In the regular expression pattern aaa(bbb)*ccc, the first capturing group (the substring "bbb") can be matched zero or more times. 由于输入字符串“aaaccc”与此模式匹配,因此该捕获组没有匹配项。Because the input string "aaaccc" matches the pattern, the capturing group does not have a match.

    using System;
    using System.Text.RegularExpressions;
    
    public class Example
    {
       public static void Main()
       {
          string pattern = "aaa(bbb)*ccc";
          string input = "aaaccc";
          Match match = Regex.Match(input, pattern);
          Console.WriteLine("Match value: {0}", match.Value);
          if (match.Groups[1].Success)
             Console.WriteLine("Group 1 value: {0}", match.Groups[1].Value);
          else
             Console.WriteLine("The first capturing group has no match.");
       }
    }
    // The example displays the following output:
    //       Match value: aaaccc
    //       The first capturing group has no match.
    
    Imports System.Text.RegularExpressions
    
    Module Example
        Public Sub Main()
            Dim pattern As String = "aaa(bbb)*ccc"
            Dim input As String = "aaaccc"
            Dim match As Match = Regex.Match(input, pattern)
            Console.WriteLine("Match value: {0}", match.Value)
            If match.Groups(1).Success Then
                Console.WriteLine("Group 1 value: {0}", match.Groups(1).Value)
            Else
                Console.WriteLine("The first capturing group has no match.")
            End If
        End Sub
    End Module
    ' The example displays the following output:
    '       Match value: aaaccc
    '       The first capturing group has no match.
    
  • 限定符可以匹配由捕获组定义的模式的多个匹配项。Quantifiers can match multiple occurrences of a pattern that is defined by a capturing group. 在此情况下,Value 对象的 LengthGroup 属性仅包含有关最后捕获的子字符串的信息。In this case, the Value and Length properties of a Group object contain information only about the last captured substring. 例如,下面的正则表达式匹配以句点结束的单个句子。For example, the following regular expression matches a single sentence that ends in a period. 此表达式使用两个分组构造:第一个分组构造捕获单个单词和空白字符;第二个分组构造捕获单个单词。It uses two grouping constructs: The first captures individual words along with a white-space character; the second captures individual words. 如示例中的输出所示,虽然正则表达式成功捕获整个句子,但第二个捕获组仅捕获了最后一个单词。As the output from the example shows, although the regular expression succeeds in capturing an entire sentence, the second capturing group captures only the last word.

    using System;
    using System.Text.RegularExpressions;
    
    public class Example
    {
       public static void Main()
       {
          string pattern = @"\b((\w+)\s?)+\.";
          string input = "This is a sentence. This is another sentence.";
          Match match = Regex.Match(input, pattern);
          if (match.Success)
          {
             Console.WriteLine("Match: " + match.Value);
             Console.WriteLine("Group 2: " + match.Groups[2].Value);
          }
       }
    }
    // The example displays the following output:
    //       Match: This is a sentence.
    //       Group 2: sentence
    
    Imports System.Text.RegularExpressions
    
    Module Example
        Public Sub Main()
            Dim pattern As String = "\b((\w+)\s?)+\."
            Dim input As String = "This is a sentence. This is another sentence."
            Dim match As Match = Regex.Match(input, pattern)
            If match.Success Then
                Console.WriteLine("Match: " + match.Value)
                Console.WriteLine("Group 2: " + match.Groups(2).Value)
            End If
        End Sub
    End Module
    ' The example displays the following output:
    '       Match: This is a sentence.
    '       Group 2: sentence
    

返回页首Back to top

捕获集合The Capture Collection

Group 对象仅包含有关最后一个捕获的信息。The Group object contains information only about the last capture. 但仍可从 CaptureCollection 属性返回的 Group.Captures 对象中获取由捕获组生成的整个捕获集。However, the entire set of captures made by a capturing group is still available from the CaptureCollection object that is returned by the Group.Captures property. 集合中的每个成员均为一个表示由该捕获组生成的捕获的 Capture 对象,这些对象按被捕获的顺序排列(因而也就是遵循在输入字符串中按从左至右匹配捕获的字符串的顺序)。Each member of the collection is a Capture object that represents a capture made by that capturing group, in the order in which they were captured (and, therefore, in the order in which the captured strings were matched from left to right in the input string). 可以通过以下两种方式之一来检索集合中的各个 Capture 对象:You can retrieve individual Capture objects from the collection in either of two ways:

  • 通过使用构造循环访问集合,如 foreach 构造(在 C# 中)或 For Each 构造(在 Visual Basic 中)。By iterating through the collection using a construct such as foreach (in C#) or For Each (in Visual Basic).

  • 通过使用 CaptureCollection.Item[] 属性按索引检索特定对象。By using the CaptureCollection.Item[] property to retrieve a specific object by index. Item[] 属性是 CaptureCollection 对象的默认属性(在 Visual Basic 中)或索引器(在 C# 中)。The Item[] property is the CaptureCollection object's default property (in Visual Basic) or indexer (in C#).

如果未对捕获组应用限定符,则 CaptureCollection 对象将包含一个 Capture 对象,但该对象的作用不大,因为它提供的是有关与其 Group 对象相同的匹配项的信息。If a quantifier is not applied to a capturing group, the CaptureCollection object contains a single Capture object that is of little interest, because it provides information about the same match as its Group object. 如果对一个捕获组应用限定符,则 CaptureCollection 对象将包含该捕获组所生成的所有捕获,并且集合的最后一个成员将表示与 Group 对象相同的捕获。If a quantifier is applied to a capturing group, the CaptureCollection object contains all captures made by the capturing group, and the last member of the collection represents the same capture as the Group object.

例如,如果使用正则表达式模式 ((a(b))c)+(其中,+ 限定符指定一个或多个匹配项)捕获字符串“abcabcabc”中的匹配项,则每个 CaptureCollection 对象的 Group 对象都将包含三个成员。For example, if you use the regular expression pattern ((a(b))c)+ (where the + quantifier specifies one or more matches) to capture matches from the string "abcabcabc", the CaptureCollection object for each Group object contains three members.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "((a(b))c)+";
      string input = "abcabcabc";

      Match match = Regex.Match(input, pattern);
      if (match.Success)
      {
         Console.WriteLine("Match: '{0}' at position {1}",
                           match.Value, match.Index);
         GroupCollection groups = match.Groups;
         for (int ctr = 0; ctr < groups.Count; ctr++) {
            Console.WriteLine("   Group {0}: '{1}' at position {2}",
                              ctr, groups[ctr].Value, groups[ctr].Index);
            CaptureCollection captures = groups[ctr].Captures;
            for (int ctr2 = 0; ctr2 < captures.Count; ctr2++) {
               Console.WriteLine("      Capture {0}: '{1}' at position {2}",
                                 ctr2, captures[ctr2].Value, captures[ctr2].Index);
            }
         }
      }
   }
}
// The example displays the following output:
//       Match: 'abcabcabc' at position 0
//          Group 0: 'abcabcabc' at position 0
//             Capture 0: 'abcabcabc' at position 0
//          Group 1: 'abc' at position 6
//             Capture 0: 'abc' at position 0
//             Capture 1: 'abc' at position 3
//             Capture 2: 'abc' at position 6
//          Group 2: 'ab' at position 6
//             Capture 0: 'ab' at position 0
//             Capture 1: 'ab' at position 3
//             Capture 2: 'ab' at position 6
//          Group 3: 'b' at position 7
//             Capture 0: 'b' at position 1
//             Capture 1: 'b' at position 4
//             Capture 2: 'b' at position 7
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim pattern As String = "((a(b))c)+"
        Dim input As STring = "abcabcabc"

        Dim match As Match = Regex.Match(input, pattern)
        If match.Success Then
            Console.WriteLine("Match: '{0}' at position {1}", _
                              match.Value, match.Index)
            Dim groups As GroupCollection = match.Groups
            For ctr As Integer = 0 To groups.Count - 1
                Console.WriteLine("   Group {0}: '{1}' at position {2}", _
                                  ctr, groups(ctr).Value, groups(ctr).Index)
                Dim captures As CaptureCollection = groups(ctr).Captures
                For ctr2 As Integer = 0 To captures.Count - 1
                    Console.WriteLine("      Capture {0}: '{1}' at position {2}", _
                                      ctr2, captures(ctr2).Value, captures(ctr2).Index)
                Next
            Next
        End If
    End Sub
End Module
' The example dosplays the following output:
'       Match: 'abcabcabc' at position 0
'          Group 0: 'abcabcabc' at position 0
'             Capture 0: 'abcabcabc' at position 0
'          Group 1: 'abc' at position 6
'             Capture 0: 'abc' at position 0
'             Capture 1: 'abc' at position 3
'             Capture 2: 'abc' at position 6
'          Group 2: 'ab' at position 6
'             Capture 0: 'ab' at position 0
'             Capture 1: 'ab' at position 3
'             Capture 2: 'ab' at position 6
'          Group 3: 'b' at position 7
'             Capture 0: 'b' at position 1
'             Capture 1: 'b' at position 4
'             Capture 2: 'b' at position 7

下面的示例使用正则表达式 (Abc)+ 来在字符串“XYZAbcAbcAbcXYZAbcAb”中查找字符串“Abc”的一个或多个连续匹配项。The following example uses the regular expression (Abc)+ to find one or more consecutive runs of the string "Abc" in the string "XYZAbcAbcAbcXYZAbcAb". 该示例演示了使用 Group.Captures 属性来返回多组捕获的子字符串。The example illustrates the use of the Group.Captures property to return multiple groups of captured substrings.

int counter;
Match m;
CaptureCollection cc;
GroupCollection gc;

// Look for groupings of "Abc".
var r = new Regex("(Abc)+");
// Define the string to search.
m = r.Match("XYZAbcAbcAbcXYZAbcAb");
gc = m.Groups;

// Display the number of groups.
Console.WriteLine("Captured groups = " + gc.Count.ToString());

// Loop through each group.
for (int i = 0; i < gc.Count; i++)
{
    cc = gc[i].Captures;
    counter = cc.Count;

    // Display the number of captures in this group.
    Console.WriteLine("Captures count = " + counter.ToString());

    // Loop through each capture in the group.
    for (int ii = 0; ii < counter; ii++)
    {
        // Display the capture and its position.
        Console.WriteLine(cc[ii] + "   Starts at character " +
             cc[ii].Index);
    }
}
// The example displays the following output:
//       Captured groups = 2
//       Captures count = 1
//       AbcAbcAbc   Starts at character 3
//       Captures count = 3
//       Abc   Starts at character 3
//       Abc   Starts at character 6
//       Abc   Starts at character 9
Dim counter As Integer
Dim m As Match
Dim cc As CaptureCollection
Dim gc As GroupCollection

' Look for groupings of "Abc".
Dim r As New Regex("(Abc)+")
' Define the string to search.
m = r.Match("XYZAbcAbcAbcXYZAbcAb")
gc = m.Groups

' Display the number of groups.
Console.WriteLine("Captured groups = " & gc.Count.ToString())

' Loop through each group.
Dim i, ii As Integer
For i = 0 To gc.Count - 1
    cc = gc(i).Captures
    counter = cc.Count

    ' Display the number of captures in this group.
    Console.WriteLine("Captures count = " & counter.ToString())

    ' Loop through each capture in the group.            
    For ii = 0 To counter - 1
        ' Display the capture and its position.
        Console.WriteLine(cc(ii).ToString() _
            & "   Starts at character " & cc(ii).Index.ToString())
    Next ii
Next i
' The example displays the following output:
'       Captured groups = 2
'       Captures count = 1
'       AbcAbcAbc   Starts at character 3
'       Captures count = 3
'       Abc   Starts at character 3
'       Abc   Starts at character 6
'       Abc   Starts at character 9  

返回页首Back to top

单个捕获The Individual Capture

Capture 类包含来自单个子表达式捕获的结果。The Capture class contains the results from a single subexpression capture. Capture.Value 属性包含匹配的文本,而 Capture.Index 属性指示匹配的子字符串在输入字符串中的起始位置(从零开始)。The Capture.Value property contains the matched text, and the Capture.Index property indicates the zero-based position in the input string at which the matched substring begins.

下面的示例分析针对选定城市的温度的输入字符串。The following example parses an input string for the temperature of selected cities. 逗号(“,”)用于将城市与其温度分隔开,而分号(“;”)用于将每个城市的数据分隔开。A comma (",") is used to separate a city and its temperature, and a semicolon (";") is used to separate each city's data. 整个输入字符串表示一个匹配项。The entire input string represents a single match. 在用于分析字符串的正则表达式模式 ((\w+(\s\w+)*),(\d+);)+ 中,城市名称将分配给第二个捕获组,而温度将分配到第四个捕获组。In the regular expression pattern ((\w+(\s\w+)*),(\d+);)+, which is used to parse the string, the city name is assigned to the second capturing group, and the temperature is assigned to the fourth capturing group.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "Miami,78;Chicago,62;New York,67;San Francisco,59;Seattle,58;";
      string pattern = @"((\w+(\s\w+)*),(\d+);)+";
      Match match = Regex.Match(input, pattern);
      if (match.Success)
      {
         Console.WriteLine("Current temperatures:");
         for (int ctr = 0; ctr < match.Groups[2].Captures.Count; ctr++)
            Console.WriteLine("{0,-20} {1,3}", match.Groups[2].Captures[ctr].Value,
                              match.Groups[4].Captures[ctr].Value);
      }
   }
}
// The example displays the following output:
//       Current temperatures:
//       Miami                 78
//       Chicago               62
//       New York              67
//       San Francisco         59
//       Seattle               58
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim input As String = "Miami,78;Chicago,62;New York,67;San Francisco,59;Seattle,58;"
        Dim pattern As String = "((\w+(\s\w+)*),(\d+);)+"
        Dim match As Match = Regex.Match(input, pattern)
        If match.Success Then
            Console.WriteLine("Current temperatures:")
            For ctr As Integer = 0 To match.Groups(2).Captures.Count - 1
                Console.WriteLine("{0,-20} {1,3}", match.Groups(2).Captures(ctr).Value, _
                                  match.Groups(4).Captures(ctr).Value)
            Next
        End If
    End Sub
End Module
' The example displays the following output:
'       Current temperatures:
'       Miami                 78
'       Chicago               62
'       New York              67
'       San Francisco         59
'       Seattle               58

该正则表达式的定义如下表所示。The regular expression is defined as shown in the following table.

模式Pattern 描述Description
\w+ 匹配一个或多个单词字符。Match one or more word characters.
(\s\w+)* 匹配零个或多个以下模式:一个空白字符后跟一个或多个单词字符。Match zero or more occurrences of a white-space character followed by one or more word characters. 此模式匹配包含多个单词的城市名称。This pattern matches multi-word city names. 这是第三个捕获组。This is the third capturing group.
(\w+(\s\w+)*) 匹配以下模式:一个或多个单词字符,后跟零个或多个一个空白字符与一个或多个单词字符的组合。Match one or more word characters followed by zero or more occurrences of a white-space character and one or more word characters. 这是第二个捕获组。This is the second capturing group.
, 匹配逗号。Match a comma.
(\d+) 匹配一个或多个数字。Match one or more digits. 这是第四个捕获组。This is the fourth capturing group.
; 匹配分号。Match a semicolon.
((\w+(\s\w+)*),(\d+);)+ 匹配一个或多个以下模式:一个单词后跟任何其他单词,后跟一个逗号、一个或多个数字和一个分号。Match the pattern of a word followed by any additional words followed by a comma, one or more digits, and a semicolon, one or more times. 这是第一个捕获组。This is the first capturing group.

请参阅See also