Capture.Value 属性

定义

获取输入的字符串中的捕获的子字符串。

public:
 property System::String ^ Value { System::String ^ get(); };
public string Value { get; }
member this.Value : string
Public ReadOnly Property Value As String

属性值

通过匹配捕获的子字符串。

示例

以下示例定义一个正则表达式,该表达式匹配除句点 (“之外不包含标点符号的句子。) 。 属性 Match.Value 显示每个匹配的结果字符串,其中包含匹配的句子。 属性 Group.Value 显示每个捕获组的结果字符串;它由该捕获组捕获的最后一个字符串组成。 属性 Capture.Value 显示每个捕获的结果字符串。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "Yes. This dog is very friendly.";
      string pattern = @"((\w+)[\s.])+";
      foreach (Match match in Regex.Matches(input, pattern))
      {
         Console.WriteLine("Match: {0}", match.Value);
         for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
         {
            Group group = match.Groups[groupCtr];
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value);
            for (int captureCtr = 0; captureCtr < group.Captures.Count; captureCtr++)
               Console.WriteLine("      Capture {0}: {1}", captureCtr, 
                                 group.Captures[captureCtr].Value);
         }                      
      }
   }
}
// The example displays the following output:
//       Match: Yes.
//          Group 0: Yes.
//             Capture 0: Yes.
//          Group 1: Yes.
//             Capture 0: Yes.
//          Group 2: Yes
//             Capture 0: Yes
//       Match: This dog is very friendly.
//          Group 0: This dog is very friendly.
//             Capture 0: This dog is very friendly.
//          Group 1: friendly.
//             Capture 0: This
//             Capture 1: dog
//             Capture 2: is
//             Capture 3: very
//             Capture 4: friendly.
//          Group 2: friendly
//             Capture 0: This
//             Capture 1: dog
//             Capture 2: is
//             Capture 3: very
//             Capture 4: friendly
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "Yes. This dog is very friendly."
      Dim pattern As String = "((\w+)[\s.])+"
      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine("Match: {0}", match.Value)
         For groupCtr As Integer = 0 To match.Groups.Count - 1
            Dim group As Group = match.Groups(groupCtr)
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value)
            For captureCtr As Integer = 0 To group.Captures.Count - 1
               Console.WriteLine("      Capture {0}: {1}", captureCtr, _
                                 group.Captures(captureCtr).Value)
            Next
         Next                      
      Next
   End Sub
End Module
' The example displays the following output:
'       Match: Yes.
'          Group 0: Yes.
'             Capture 0: Yes.
'          Group 1: Yes.
'             Capture 0: Yes.
'          Group 2: Yes
'             Capture 0: Yes
'       Match: This dog is very friendly.
'          Group 0: This dog is very friendly.
'             Capture 0: This dog is very friendly.
'          Group 1: friendly.
'             Capture 0: This
'             Capture 1: dog
'             Capture 2: is
'             Capture 3: very
'             Capture 4: friendly.
'          Group 2: friendly
'             Capture 0: This
'             Capture 1: dog
'             Capture 2: is
'             Capture 3: very
'             Capture 4: friendly

正则表达式模式 ((\w+)[\s.])+ 的定义如下表所示。 请注意,在此正则表达式中,限定符 (+) 应用于整个正则表达式。

模式 描述
(\w+) 匹配一个或多个单词字符。 这是第二个捕获组。
[\s.]) 匹配空白字符或句点 (“。”) 。
((\w+)[\s.]) 匹配一个或多个单词字符,后跟空格字符或句点 (”。) 。 这是第一个捕获组。
((\w+)[\s.])+ 匹配单词字符或后跟空格字符或句点的一个或多个匹配项, (”。) 。

在此示例中,输入字符串由两个句子组成。 如输出所示,第一个句子仅包含一个单词,因此该 CaptureCollection 对象具有一 Capture 个对象,该对象表示与对象 Group 相同的捕获。 第二个句子由多个单词组成,因此对象 Group 仅包含有关最后一个匹配子表达式的信息。 组 1 表示第一次捕获,包含句子中最后一个具有结束句点的单词。 第 2 组(表示第二个捕获)包含句子中的最后一个单词。 但是, CaptureCaptureCollection 的 对象中的 对象捕获每个子表达式匹配项。 Capture第一个捕获组的捕获集合中的对象包含有关每个捕获的单词和空白字符或句点的信息。 Capture第二个捕获组的捕获集合中的对象包含有关每个捕获字词的信息。

以下示例使用正则表达式模式 ^([a-z]+)(\d+)*\.([a-z]+(\d)*)$来匹配由句点分隔的两个部分组成的产品代码。 这两个部分都由字母字符组成,后跟可选数字。 由于第一个输入字符串与模式不匹配,因此返回 System.Text.RegularExpressions.Match 对象的 Value 属性值为 String.Empty。 同样,当正则表达式模式无法匹配捕获组时,相应 Group 对象的 Value 属性 String.Empty的值为 。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      String pattern = @"^([a-z]+)(\d+)?\.([a-z]+(\d)*)$";
      String[] values = { "AC10", "Za203.CYM", "XYZ.CoA", "ABC.x170" };   
      foreach (var value in values) {
         Match m = Regex.Match(value, pattern, RegexOptions.IgnoreCase);
         if (m.Success) {
            Console.WriteLine("Match: '{0}'", m.Value);
            Console.WriteLine("   Number of Capturing Groups: {0}", 
                              m.Groups.Count);
            for (int gCtr = 0; gCtr < m.Groups.Count; gCtr++) {
               Group group = m.Groups[gCtr];
               Console.WriteLine("      Group {0}: {1}", gCtr, 
                                 group.Value == "" ? "<empty>" : "'" + group.Value + "'");   
               Console.WriteLine("         Number of Captures: {0}", 
                                 group.Captures.Count);
           
               for (int cCtr = 0; cCtr < group.Captures.Count; cCtr++) 
                  Console.WriteLine("            Capture {0}: {1}", 
                                    cCtr, group.Captures[cCtr].Value);
            }
         } 
         else {
            Console.WriteLine("No match for {0}: Match.Value is {1}", 
                              value, m.Value == String.Empty ? "<empty>" : m.Value);
         }
      }
   }
}
// The example displays the following output:
//       No match for AC10: Match.Value is <empty>
//       Match: 'Za203.CYM'
//          Number of Capturing Groups: 5
//             Group 0: 'Za203.CYM'
//                Number of Captures: 1
//                   Capture 0: Za203.CYM
//             Group 1: 'Za'
//                Number of Captures: 1
//                   Capture 0: Za
//             Group 2: '203'
//                Number of Captures: 1
//                   Capture 0: 203
//             Group 3: 'CYM'
//                Number of Captures: 1
//                   Capture 0: CYM
//             Group 4: <empty>
//                Number of Captures: 0
//       Match: 'XYZ.CoA'
//          Number of Capturing Groups: 5
//             Group 0: 'XYZ.CoA'
//                Number of Captures: 1
//                   Capture 0: XYZ.CoA
//             Group 1: 'XYZ'
//                Number of Captures: 1
//                   Capture 0: XYZ
//             Group 2: <empty>
//                Number of Captures: 0
//             Group 3: 'CoA'
//                Number of Captures: 1
//                   Capture 0: CoA
//             Group 4: <empty>
//                Number of Captures: 0
//       Match: 'ABC.x170'
//          Number of Capturing Groups: 5
//             Group 0: 'ABC.x170'
//                Number of Captures: 1
//                   Capture 0: ABC.x170
//             Group 1: 'ABC'
//                Number of Captures: 1
//                   Capture 0: ABC
//             Group 2: <empty>
//                Number of Captures: 0
//             Group 3: 'x170'
//                Number of Captures: 1
//                   Capture 0: x170
//             Group 4: '0'
//                Number of Captures: 3
//                   Capture 0: 1
//                   Capture 1: 7
//                   Capture 2: 0
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "^([a-z]+)(\d+)?\.([a-z]+(\d)*)$"
      Dim values() As String = { "AC10", "Za203.CYM", "XYZ.CoA", "ABC.x170" }   
      For Each value In values
         Dim m As Match = Regex.Match(value, pattern, RegexOptions.IgnoreCase)
         If m.Success Then
            Console.WriteLine("Match: '{0}'", m.Value)
            Console.WriteLine("   Number of Capturing Groups: {0}", 
                              m.Groups.Count)
            For gCtr As Integer = 0 To m.Groups.Count - 1
               Dim group As Group = m.Groups(gCtr)
               Console.WriteLine("      Group {0}: {1}", gCtr, 
                                 If(group.Value = "", "<empty>", "'" + group.Value + "'"))   
               Console.WriteLine("         Number of Captures: {0}", 
                                 group.Captures.Count)
           
               For cCtr As Integer = 0 To group.Captures.Count - 1
                  Console.WriteLine("            Capture {0}: {1}", 
                                    cCtr, group.Captures(cCtr).Value)
               Next
            Next
         Else
            Console.WriteLine("No match for {0}: Match.Value is {1}", 
                              value, If(m.Value = String.Empty, "<empty>", m.Value))
         End If
      Next    
   End Sub
End Module
' The example displays the following output:
'       No match for AC10: Match.Value is <empty>
'       Match: 'Za203.CYM'
'          Number of Capturing Groups: 5
'             Group 0: 'Za203.CYM'
'                Number of Captures: 1
'                   Capture 0: Za203.CYM
'             Group 1: 'Za'
'                Number of Captures: 1
'                   Capture 0: Za
'             Group 2: '203'
'                Number of Captures: 1
'                   Capture 0: 203
'             Group 3: 'CYM'
'                Number of Captures: 1
'                   Capture 0: CYM
'             Group 4: <empty>
'                Number of Captures: 0
'       Match: 'XYZ.CoA'
'          Number of Capturing Groups: 5
'             Group 0: 'XYZ.CoA'
'                Number of Captures: 1
'                   Capture 0: XYZ.CoA
'             Group 1: 'XYZ'
'                Number of Captures: 1
'                   Capture 0: XYZ
'             Group 2: <empty>
'                Number of Captures: 0
'             Group 3: 'CoA'
'                Number of Captures: 1
'                   Capture 0: CoA
'             Group 4: <empty>
'                Number of Captures: 0
'       Match: 'ABC.x170'
'          Number of Capturing Groups: 5
'             Group 0: 'ABC.x170'
'                Number of Captures: 1
'                   Capture 0: ABC.x170
'             Group 1: 'ABC'
'                Number of Captures: 1
'                   Capture 0: ABC
'             Group 2: <empty>
'                Number of Captures: 0
'             Group 3: 'x170'
'                Number of Captures: 1
'                   Capture 0: x170
'             Group 4: '0'
'                Number of Captures: 3
'                   Capture 0: 1
'                   Capture 1: 7
'                   Capture 2: 0

正则表达式模式的定义如下表所示:

模式 描述
^ 从字符串开头开始匹配。
([a-z]+) 匹配从 到 z 的任何字符的一个或多个匹配项。 由于正则表达式引擎传递 了 RegexOptions.IgnoreCase 选项,因此此比较不区分大小写。 这是第一个捕获组。
(\d+)? 匹配一个或多个十进制数字的零个或一个匹配项。 这是第二个捕获组。
\. 匹配文本句点字符。
([a-z]+ 匹配从 到 z 的任何字符的一个或多个匹配项。 比较不区分大小写。
(\d)* 匹配零个或多个十进制数字。 一个匹配的数字是第四个捕获组。
([a-z]+(\d)*) 匹配从 到 z 的一个或多个字母字符,后跟零、一个或多个十进制数字。 这是第四个捕获组。
$ 结束字符串末尾的匹配。

注解

如果对 或 Match.NextMatch 方法的Regex.Match调用找不到匹配项,则返回Match.Value的属性String.Empty的值为 。 如果正则表达式引擎无法匹配捕获组。 返回 Group.Value 的属性 String.Empty的值为 。 有关插图,请参阅第二个示例。

适用于