How Do I...Get all matches for a pattern
Regular Expressions are often useful when trying to retrieve small portions of
text from a large document, result set, or when filtering a stream. The MatchCollection
object contains all valid Match objects for a given regular expression after a
successful match occurs.
Regex digitregex = new Regex("(?<number>\\d+)");
String s = "abc 123 def 456 ghi 789";
...
MatchCollection ms = digitregex.Matches(s);
C#
The following example illustrates how to create a Regex that matches numbers in a string.
The Matches method is callled to return a MatchCollection. If the Count
property equals 0, no successful matches have occured. If there are matches, the results
of each are displayed.
Example
C# RegexMatches.exe
[This sample can be found at M:\web\users\Sites\AspnetQuickStart\v2.0\QuickStart\howto\samples\RegularExpressions\RegexMatches\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild
passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin
directory.]
Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2005 Microsoft Corporation. All rights reserved.
|