Regex::Matches Method (String, Int32)
Searches the specified input string for all occurrences of a regular expression, beginning at the specified starting position in the string.
Assembly: System (in System.dll)
- Type: System::String
The string to search for a match.
- Type: System::Int32
The character position in the input string at which to start the search.
The method is similar to the Match method, except that it returns information about all the matches, instead of a single match, found in the input string. It is equivalent to the following code:
The collection includes only successful matches and terminates at the first unsuccessful match.
The regular expression pattern for which the method searches is defined by the call to one of the Regex class constructors. For more information about the elements that can form a regular expression pattern, see Regular Expression Language - Quick Reference.
The Matches method uses lazy evaluation to populate the returned MatchCollection object. Accessing such members of this collection as MatchCollection::Count and MatchCollection::CopyTo causes the collection to be populated immediately. To take advantage of lazy evaluation, you should iterate the collection by using a construct such as foreach in C# and For Each…Next in Visual Basic.
The following example uses the Match(String) method to find the first word in a sentence that ends in "es", and then calls the method to identify any additional words that end in "es".
The regular expression pattern \b\w+es\b is defined as shown in the following table.
Begin the match at a word boundary.
Match one or more word characters.
Match the literal string "es".
End the match at a word boundary.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.