Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

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

VB RegexMatches.exe
View Source
[This sample can be found at C:\DevFusion.Data\legacy\quickstart.developerfusion.co.uk\QuickStart\howto\samples\RegularExpressions\RegexMatches\]



Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.