CaptureCollection 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示单个捕获组进行的捕获集。 集合不可变(只读),没有公共构造函数。
public ref class CaptureCollection : System::Collections::ICollection
public ref class CaptureCollection : System::Collections::Generic::ICollection<System::Text::RegularExpressions::Capture ^>, System::Collections::Generic::IEnumerable<System::Text::RegularExpressions::Capture ^>, System::Collections::Generic::IList<System::Text::RegularExpressions::Capture ^>, System::Collections::Generic::IReadOnlyCollection<System::Text::RegularExpressions::Capture ^>, System::Collections::Generic::IReadOnlyList<System::Text::RegularExpressions::Capture ^>, System::Collections::IList
public class CaptureCollection : System.Collections.ICollection
public class CaptureCollection : System.Collections.Generic.ICollection<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IEnumerable<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IList<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IReadOnlyCollection<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IReadOnlyList<System.Text.RegularExpressions.Capture>, System.Collections.IList
[System.Serializable]
public class CaptureCollection : System.Collections.ICollection
type CaptureCollection = class
interface ICollection
interface IEnumerable
type CaptureCollection = class
interface ICollection<Capture>
interface seq<Capture>
interface IEnumerable
interface IList<Capture>
interface IReadOnlyCollection<Capture>
interface IReadOnlyList<Capture>
interface ICollection
interface IList
type CaptureCollection = class
interface ICollection
interface IEnumerable
interface ICollection<Capture>
interface seq<Capture>
interface IList<Capture>
interface IReadOnlyCollection<Capture>
interface IReadOnlyList<Capture>
interface IList
[<System.Serializable>]
type CaptureCollection = class
interface ICollection
interface IEnumerable
Public Class CaptureCollection
Implements ICollection
Public Class CaptureCollection
Implements ICollection(Of Capture), IEnumerable(Of Capture), IList, IList(Of Capture), IReadOnlyCollection(Of Capture), IReadOnlyList(Of Capture)
- 继承
-
CaptureCollection
- 属性
- 实现
示例
以下示例比较 Group.Captures 和 Match.Captures
属性返回的 CaptureCollection 对象中的 Capture 对象。 它还将 Group 对象与 Group.Captures 属性返回的 CaptureCollection 中的 Capture 对象进行比较。 该示例使用以下两个正则表达式在单个输入字符串中查找匹配项:
\b\w+\W{1,2}
此正则表达式模式标识包含一个或多个单词字符的单词,后跟一两个非单词字符,例如空格或标点符号。 正则表达式不包含任何捕获组。 该示例的输出显示 Match 对象以及 Group.Captures 返回的 CaptureCollection 对象以及
Match.Captures
属性包含有关相同匹配的信息。(\b\w+\W{1,2})+
此正则表达式模式标识句子中的单词。 该模式定义一个捕获组,其中包含一个或多个单词字符,后跟一两个非单词字符。 正则表达式模式使用
+
限定符来匹配此组的一个或多个匹配项。 此示例的输出显示 Match 对象和Match.Captures
属性返回的 CaptureCollection 对象包含有关相同匹配的信息。 第二个 Group 对象(对应于正则表达式中唯一的捕获组)仅标识最后一个捕获的字符串,而第一个捕获组的 Group.Captures 属性返回的 CaptureCollection 对象包括所有捕获的子字符串。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern;
string input = "The young, hairy, and tall dog slowly walked across the yard.";
Match match;
// Match a word with a pattern that has no capturing groups.
pattern = @"\b\w+\W{1,2}";
match = Regex.Match(input, pattern);
Console.WriteLine("Pattern: " + pattern);
Console.WriteLine("Match: " + match.Value);
Console.WriteLine(" Match.Captures: {0}", match.Captures.Count);
for (int ctr = 0; ctr < match.Captures.Count; ctr++)
Console.WriteLine(" {0}: '{1}'", ctr, match.Captures[ctr].Value);
Console.WriteLine(" Match.Groups: {0}", match.Groups.Count);
for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
{
Console.WriteLine(" Group {0}: '{1}'",
groupCtr, match.Groups[groupCtr].Value);
Console.WriteLine(" Group({0}).Captures: {1}",
groupCtr, match.Groups[groupCtr].Captures.Count);
for (int captureCtr = 0; captureCtr < match.Groups[groupCtr].Captures.Count; captureCtr++)
Console.WriteLine(" Capture {0}: '{1}'",
captureCtr,
match.Groups[groupCtr].Captures[captureCtr].Value);
}
Console.WriteLine("-----\n");
// Match a sentence with a pattern that has a quantifier that
// applies to the entire group.
pattern = @"(\b\w+\W{1,2})+";
match = Regex.Match(input, pattern);
Console.WriteLine("Pattern: " + pattern);
Console.WriteLine("Match: " + match.Value);
Console.WriteLine(" Match.Captures: {0}", match.Captures.Count);
for (int ctr = 0; ctr < match.Captures.Count; ctr++)
Console.WriteLine(" {0}: '{1}'", ctr, match.Captures[ctr].Value);
Console.WriteLine(" Match.Groups: {0}", match.Groups.Count);
for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
{
Console.WriteLine(" Group {0}: '{1}'", groupCtr, match.Groups[groupCtr].Value);
Console.WriteLine(" Group({0}).Captures: {1}",
groupCtr, match.Groups[groupCtr].Captures.Count);
for (int captureCtr = 0; captureCtr < match.Groups[groupCtr].Captures.Count; captureCtr++)
Console.WriteLine(" Capture {0}: '{1}'", captureCtr, match.Groups[groupCtr].Captures[captureCtr].Value);
}
}
}
// The example displays the following output:
// Pattern: \b\w+\W{1,2}
// Match: The
// Match.Captures: 1
// 0: 'The '
// Match.Groups: 1
// Group 0: 'The '
// Group(0).Captures: 1
// Capture 0: 'The '
// -----
//
// Pattern: (\b\w+\W{1,2})+
// Match: The young, hairy, and tall dog slowly walked across the yard.
// Match.Captures: 1
// 0: 'The young, hairy, and tall dog slowly walked across the yard.'
// Match.Groups: 2
// Group 0: 'The young, hairy, and tall dog slowly walked across the yard.'
// Group(0).Captures: 1
// Capture 0: 'The young, hairy, and tall dog slowly walked across the yard.'
// Group 1: 'yard.'
// Group(1).Captures: 11
// Capture 0: 'The '
// Capture 1: 'young, '
// Capture 2: 'hairy, '
// Capture 3: 'and '
// Capture 4: 'tall '
// Capture 5: 'dog '
// Capture 6: 'slowly '
// Capture 7: 'walked '
// Capture 8: 'across '
// Capture 9: 'the '
// Capture 10: 'yard.'
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String
Dim input As String = "The young, hairy, and tall dog slowly walked across the yard."
Dim match As Match
' Match a word with a pattern that has no capturing groups.
pattern = "\b\w+\W{1,2}"
match = Regex.Match(input, pattern)
Console.WriteLine("Pattern: " + pattern)
Console.WriteLine("Match: " + match.Value)
Console.WriteLine(" Match.Captures: {0}", match.Captures.Count)
For ctr As Integer = 0 To match.Captures.Count - 1
Console.WriteLine(" {0}: '{1}'", ctr, match.Captures(ctr).Value)
Next
Console.WriteLine(" Match.Groups: {0}", match.Groups.Count)
For groupCtr As Integer = 0 To match.Groups.Count - 1
Console.WriteLine(" Group {0}: '{1}'", groupCtr, match.Groups(groupCtr).Value)
Console.WriteLine(" Group({0}).Captures: {1}", _
groupCtr, match.Groups(groupCtr).Captures.Count)
For captureCtr As Integer = 0 To match.Groups(groupCtr).Captures.Count - 1
Console.WriteLine(" Capture {0}: '{1}'", _
captureCtr, _
match.Groups(groupCtr).Captures(captureCtr).Value)
Next
Next
Console.WriteLine("-----")
Console.WriteLine()
' Match a sentence with a pattern that has a quantifier that
' applies to the entire group.
pattern = "(\b\w+\W{1,2})+"
match = Regex.Match(input, pattern)
Console.WriteLine("Pattern: " + pattern)
Console.WriteLine("Match: " + match.Value)
Console.WriteLine(" Match.Captures: {0}", match.Captures.Count)
For ctr As Integer = 0 To match.Captures.Count - 1
Console.WriteLine(" {0}: '{1}'", ctr, match.Captures(ctr).Value)
Next
Console.WriteLine(" Match.Groups: {0}", match.Groups.Count)
For groupCtr As Integer = 0 To match.Groups.Count - 1
Console.WriteLine(" Group {0}: '{1}'", groupCtr, match.Groups(groupCtr).Value)
Console.WriteLine(" Group({0}).Captures: {1}", _
groupCtr, match.Groups(groupCtr).Captures.Count)
For captureCtr As Integer = 0 To match.Groups(groupCtr).Captures.Count - 1
Console.WriteLine(" Capture {0}: '{1}'", captureCtr, match.Groups(groupCtr).Captures(captureCtr).Value)
Next
Next
End Sub
End Module
' The example displays the following output:
' Pattern: \b\w+\W{1,2}
' Match: The
' Match.Captures: 1
' 0: 'The '
' Match.Groups: 1
' Group 0: 'The '
' Group(0).Captures: 1
' Capture 0: 'The '
' -----
'
' Pattern: (\b\w+\W{1,2})+
' Match: The young, hairy, and tall dog slowly walked across the yard.
' Match.Captures: 1
' 0: 'The young, hairy, and tall dog slowly walked across the yard.'
' Match.Groups: 2
' Group 0: 'The young, hairy, and tall dog slowly walked across the yard.'
' Group(0).Captures: 1
' Capture 0: 'The young, hairy, and tall dog slowly walked across the yard.'
' Group 1: 'yard.'
' Group(1).Captures: 11
' Capture 0: 'The '
' Capture 1: 'young, '
' Capture 2: 'hairy, '
' Capture 3: 'and '
' Capture 4: 'tall '
' Capture 5: 'dog '
' Capture 6: 'slowly '
' Capture 7: 'walked '
' Capture 8: 'across '
' Capture 9: 'the '
' Capture 10: 'yard.'
注解
集合不可变(只读),没有公共构造函数。 CaptureCollection 对象包含一个或多个 Capture 对象。
CaptureCollection 类的实例由以下属性返回:
Group.Captures 属性。 集合的每个成员都表示由捕获组捕获的子字符串。 如果未将限定符应用于捕获组,则 CaptureCollection 包括一个表示与 Group 对象相同的捕获子字符串的单个 Capture 对象。 如果限定符应用于捕获组,则 CaptureCollection 为每个捕获的子字符串包含一个 Capture 对象,Group 对象仅提供有关上次捕获的子字符串的信息。
Match.Captures
属性。 在这种情况下,集合由单个 Capture 对象组成,该对象提供有关整个匹配的信息。 也就是说,CaptureCollection 对象提供的信息与 Match 对象相同。
若要循环访问集合的成员,应使用语言提供的集合迭代构造(如 C# 中的 foreach
和 Visual Basic 中的 For Each
...Next
),而不是检索由 GetEnumerator 方法返回的枚举器。
属性
Count |
获取组捕获的子字符串数。 |
IsReadOnly |
获取一个值,该值指示集合是否为只读。 |
IsSynchronized |
获取一个值,该值指示是否同步对集合的访问(线程安全)。 |
Item[Int32] |
获取集合的单个成员。 |
SyncRoot |
获取一个对象,该对象可用于同步对集合的访问。 |
方法
CopyTo(Array, Int32) |
将集合的所有元素复制到从给定索引处开始的给定数组。 |
CopyTo(Capture[], Int32) | |
Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
GetEnumerator() |
提供循环访问集合的枚举器。 |
GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |