Groups

Consider the following regular expression that represents a U.S. phone number such as 222-444-1111:


\d{3}-\d{3}-\d{4}

Suppose we wish to separate this into two groups: area code and local number.

We can achieve this by using parentheses to capture each group:


(\d{3})-(\d{3}-\d{4})

SyntaxMeaning
(expr)Capture matched expression expr into indexed group
(?number)Capture matched substring into a specified group number
(?'name')Capture matched substring into group name
(?'name1-name2')Undefinename2, and store interval and current group into name1; if name2 is undefined, matching backtracks;
(?:expr)Noncapturing group

Back references

Parameter syntaxMeaning
\indexReference a previously captured group by index
\k<name>Reference a previously captured group by name

We then retrieve the groups programmatically as follows:


using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {

       Match m = Regex.Match ("222-444-1111", @"(\d{3})-(\d{3}-\d{4})"); 
  
       Console.WriteLine (m.Groups[1]);    
       Console.WriteLine (m.Groups[2]);    
   }
}

The output:


222
444-1111

The zeroth group represents the entire match.

In other words, it has the same value as the match's Value:


using System;
using System.Text.RegularExpressions;


class Program
{
    static void Main(string[] args)
    {
    
         Match m = Regex.Match ("222-444-1111", @"(\d{3})-(\d{3}-\d{4})"); 
    
         Console.WriteLine (m.Groups[0]);           
         Console.WriteLine (m);                     
    }
}

The output:


222-444-1111
222-444-1111

You can refer to a group within a regular expression.

The \n syntax lets you index the group by group number n within the expression.

In the following example, we find all words in a string starting and ending in the same letter:


using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {

         foreach (Match m in Regex.Matches ("bee cake level bob", @"\b(\w)\w+\1\b")) 
            Console.Write (m + " ");       
    }
}

The output:


level bob
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.