Gets a collection of groups matched by the regular expression. : Regular Expression « Development Class « C# / C Sharp






Gets a collection of groups matched by the regular expression.

 

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"(\d{3})-(\d{3}-\d{4})";
      string input = "111-222-1111 222-111-1111 333-222-3333 444-888-9999";
      MatchCollection matches = Regex.Matches(input, pattern);

      foreach (Match match in matches)
      {
         Console.WriteLine("Area Code:        {0}", match.Groups[1].Value);
         Console.WriteLine("Telephone number: {0}", match.Groups[2].Value);
         Console.WriteLine();
      }
   }
}

   
  








Related examples in the same category

1.Multiple different delimiters
2.Single space split
3.Multiple spaces, using \"[\\s]+\"
4.Single spaces, using ()
5.Single spaces, using new Regex("( )")
6.Position and index
7.Regex and letter case
8.((an)|(in)|(on))
9.Gr(a|e)y
10.Define groups 'ing', 'in', 'n'
11.Leading space
12.Proper case match
13.Use | to combine different case
14.IP address
15.Multiple spaces, using \" \"
16.Use regular expression to replace charsUse regular expression to replace chars
17.Use regular to replace stringsUse regular to replace strings
18.Use regular to replace wordUse regular to replace word
19.Use regular expression to replace first 3 digitsUse regular expression to replace first 3 digits
20.Use regular to split stringUse regular to split string
21.Validate first name and last name
22.Validate address
23.Validate city name
24.Validate ZIP code
25.Validate Phone Number
26.Use regular to verify a dateUse regular to verify a date
27.HTML Parser
28.Define a regular expression for repeated wordsDefine a regular expression for repeated words
29.Gets the group name that corresponds to the specified group number.
30.Indicates whether the regular expression specified in the Regex constructor finds a match in the input string.
31.Regex.IsMatch Method
32.Regex.IsMatch Method (String, String, RegexOptions)
33.Match Class represents the results from a single regular expression match.
34.Get MatchCollection
35.Is match successful
36.Get CaptureCollection
37.Returns a new Match object with the results for the next match, starting at the position at which the last match ended
38.Returns the expansion of the specified replacement pattern.
39.Represents the set of successful matches found by iteratively applying a regular expression pattern to the input string.Represents the set of successful matches found by iteratively applying a regular expression pattern to the input string.
40.Gets an individual member of the collection.