MatchCollection.GetEnumerator 方法

定义

提供一个循环访问集合的枚举器。Provides an enumerator that iterates through the collection.

public:
 virtual System::Collections::IEnumerator ^ GetEnumerator();
public System.Collections.IEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IEnumerator
override this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Function GetEnumerator () As IEnumerator

返回

IEnumerator

包含 Match 中所有 MatchCollection 对象的对象。An object that contains all Match objects within the MatchCollection.

实现

例外

发生超时。A time-out occurred.

注解

GetEnumerator Match 应使用组迭代构造 (如 foreach c # 和 ... 中的,而不是调用方法来检索枚举器,以使您能够循环访问集合中的对象 For EachNextInstead of calling the GetEnumerator method to retrieve an enumerator that lets you iterate through the Match objects in the collection, you should use the group iteration construct (such as foreach in C# and For EachNext 用编程语言提供的 Visual Basic) 。in Visual Basic) provided by your programming language.

MatchCollection使用 GetEnumerator 方法 (或 foreach c # 中的语句或 Visual Basic) 中的语句来循环访问对象的成员,将 For Each Next 导致正则表达式引擎使用延迟计算根据需要填充集合。Iterating the members of the MatchCollection object using the GetEnumerator method (or the foreach statement in C# and the For Each...Next statement in Visual Basic) causes the regular expression engine to populate the collection on an as needed basis using lazy evaluation. 这类似于重复调用 Regex.Match 方法,然后将生成的匹配项添加到 MatchCollection 对象。This is analogous to repeatedly calling the Regex.Match method, and then adding the resulting match to the MatchCollection object. 与此相反,正则表达式引擎在访问属性时使用直接计算一次填充集合 CountIn contrast, the regular expression engine uses direct evaluation to populate the collection all at once when the Count property is accessed. 这种生成集合的方法比延迟计算要大得多。This can be a much more expensive method of building the collection than lazy evaluation.

由于 MatchCollection 通常使用延迟计算来填充对象,因此尝试导航到集合的下一个成员可能会引发 RegexMatchTimeoutException 异常。Because the MatchCollection object is generally populated by using lazy evaluation, trying to navigate to the next member of the collection may throw a RegexMatchTimeoutException exception. 如果匹配操作的超时值有效,且尝试查找下一个匹配项,则会引发此异常。This exception can be thrown if a time-out value for matching operations is in effect, and the attempt to find the next match exceeds that time-out interval.

适用于