Capture.Value 属性

定义

获取输入的字符串中的捕获的子字符串。Gets the captured substring from the input string.

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

属性值

通过匹配捕获的子字符串。The substring that is captured by the match.

示例

下面的示例定义一个正则表达式, 该正则表达式匹配不包含句点 (".") 以外的任何标点的句子。The following example defines a regular expression that matches sentences that contain no punctuation except for a period ("."). Match.Value属性显示每个匹配项的结果字符串, 其中包含匹配的句子。The Match.Value property displays the result string, which consists of a matched sentence, for each match. Group.Value属性显示每个捕获组的结果字符串; 它包含由该捕获组捕获的最后一个字符串。The Group.Value property displays the result string for each capturing group; it consists of the last string captured by that capturing group. Capture.Value属性显示每个捕获的结果字符串。The Capture.Value property displays the result string for each capture.

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.])+ 的定义如下表所示。The regular expression pattern ((\w+)[\s.])+ is defined as shown in the following table. 请注意, 在此正则表达式中, 限定符 (+) 应用于整个正则表达式。Note that in this regular expression, a quantifier (+) is applied to the entire regular expression.

模式Pattern 说明Description
(\w+) 匹配一个或多个单词字符。Match one or more word characters. 这是第二个捕获组。This is the second capturing group.
[\s.]) 匹配空白字符或句点 (".")。Match a white-space character or period (".").
((\w+)[\s.]) 匹配一个或多个单词字符, 后跟一个空白字符或句点 (".")。Match one or more word characters followed by a white-space character or period ("."). 这是第一个捕获组。This is the first capturing group.
((\w+)[\s.])+ 匹配一个或多个单词字符, 后跟一个空白字符或句点 (".")。Match one or more occurrences of a word character or characters followed by a white-space character or period (".").

在此示例中, 输入字符串包含两个句子。In this example, the input string consists of two sentences. 如输出所示, 第一句只包含一个单词, 因此该CaptureCollection对象有Capture一个对象, 该对象表示与Group对象相同的捕获。As the output shows, the first sentence consists of only one word, so the CaptureCollection object has a single Capture object that represents the same capture as the Group object. 第二个句子包含多个单词, 因此Group对象只包含与最后一个匹配的子表达式有关的信息。The second sentence consists of multiple words, so the Group objects only contain information about the last matched subexpression. 组 1, 表示第一个捕获, 包含具有结束时间的句子中的最后一个单词。Group 1, which represents the first capture, contains the last word in the sentence that has a closing period. 组2表示第二个捕获, 其中包含句子中的最后一个单词。Group 2, which represents the second capture, contains the last word in the sentence. 但组的Capture CaptureCollection对象中的对象将捕获每个子表达式匹配项。However, the Capture objects in the group's CaptureCollection object capture each subexpression match. Capture一个捕获组的捕获集合中的对象包含有关每个捕获的单词和空白字符或句点的信息。The Capture objects in the first capturing group's collection of captures contain information about each captured word and white-space character or period. 第二个捕获组的捕获集合中的对象包含有关每个捕获的字词的信息。CaptureThe Capture objects in the second capturing group's collection of captures contain information about each captured word.

下面的示例使用正则表达式模式^([a-z]+)(\d+)*\.([a-z]+(\d)*)$来匹配由以句点分隔的两部分组成的产品编号。The following example uses a regular expression pattern, ^([a-z]+)(\d+)*\.([a-z]+(\d)*)$, to match a product number that consists of two parts separated by a period. 这两个部分由字母字符后跟可选数字组成。Both parts consist of alphabetic characters followed by optional numbers. 由于第一个输入字符串与模式不匹配, 因此返回System.Text.RegularExpressions.Match Value对象的属性的值为String.EmptyBecause the first input string does not match the pattern, the value of the returned System.Text.RegularExpressions.Match object's Value property is String.Empty. 同样, 如果正则表达式模式与捕获组不匹配, 则相应Group Value对象的属性的值为String.EmptySimilarly, when the regular expression pattern is unable to match a capturing group, the value of the corresponding Group object's Value property is 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

正则表达式模式的定义如下表所示:The regular expression pattern is defined as shown in the following table:

模式Pattern 说明Description
^ 从字符串开头开始匹配。Begin the match at the beginning of the string.
([a-z]+) 匹配从 a 到 z 的任意字符的一个或多个匹配项。Match one or more occurrences of any character from a to z. 因为正则表达式引擎是通过选项RegexOptions.IgnoreCase传递的, 所以此比较不区分大小写。Because the regular expression engine is passed the RegexOptions.IgnoreCase option, this comparison is case-insensitive. 这是第一个捕获组。This is the first capturing group.
(\d+)? 匹配一个或多个十进制数字的零个或一个匹配项。Match zero or one occurrence of one or more decimal digits. 这是第二个捕获组。This is the second capturing group.
\. 匹配文本句点字符。Match a literal period character.
([a-z]+ 匹配从 a 到 z 的任意字符的一个或多个匹配项。Match one or more occurrences of any character from a to z. 比较时不区分大小写。The comparison is case-insensitive.
(\d)* 匹配零个或多个十进制数字。Match zero or more decimal digits. 单个匹配的数字是第四个捕获组。A single matched digit is the fourth capturing group.
([a-z]+(\d)*) 匹配从 a 到 z 的一个或多个字母字符, 后跟零个、一个或多个十进制数字。Match one or more alphabetic characters from a to z followed by zero, one, or more decimal digits. 这是第四个捕获组。This is the fourth capturing group.
$ 在字符串末尾结束匹配。Conclude the match at the end of the string.

注解

如果调用Regex.MatchMatch.NextMatch方法未能找到匹配项, 则返回Match.Value的属性的值为String.EmptyIf a call to the Regex.Match or Match.NextMatch method fails to find a match, the value of the returned Match.Value property is String.Empty. 如果正则表达式引擎不能与捕获组匹配, 则为。If the regular expression engine is unable to match a capturing group. 返回Group.Value的属性的值为String.Emptythe value of the returned Group.Value property is String.Empty. 有关说明, 请参阅第二个示例。See the second example for an illustration.

适用于