Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

MatchCollection::Count Property

 

Gets the number of matches.

Namespace:   System.Text.RegularExpressions
Assembly:  System (in System.dll)

public:
property int Count {
	virtual int get() sealed;
}

Property Value

Type: System::Int32

The number of matches.

Exception Condition
RegexMatchTimeoutException

A time-out occurred.

Accessing individual members of the MatchCollection object by retrieving the value of the collection's Count property causes the regular expression engine to populate the collection using direct evaluation. ln contrast, calling the GetEnumerator method (or using 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. Direct evaluation can be a much more expensive method of building the collection than lazy evaluation.

Because the MatchCollection object is generally populated by using lazy evaluation, trying to determine the number of elements in the collection before it has been fully populated 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 a single match exceeds that time-out interval.

The following example uses the Count property to determine whether the call to the Regex::Matches(String^, String^) method found any matches. If not, it indicates that no matches were found. Otherwise, it enumerates the matches and displays their value and the position in the input string at which they were found.

No code example is currently available or this language may not be supported.

The regular expression pattern \d+ matches one or more decimal characters in an input string.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2017 Microsoft