In answering posts to MSDN’s regex forums I find that I have to display the match named capture groups in a consistent fashion. Here is code which will extract all the named capture groups from the regex and then enumerate over the the matches and display all the named and unnamed capture groups and values if they have them.

Here is the static I use to report those groups by passing in the text to work off of and the regex object:

private static void ReportGroupsAndOutput( string text, Regex rgx )

    string[] groupNames = rgx.GetGroupNames();

    Console.WriteLine("Groups: ({0}){1}", string.Join(") (", groupNames), System.Environment.NewLine);

    MatchCollection mc = rgx.Matches(text);

    foreach (Match m in mc)

        if (m.Success)

            foreach (string name in groupNames)
                Console.WriteLine("{0,10} : {1}", name, m.Groups[name]);

            Console.WriteLine("{0}", System.Environment.NewLine);


The result would look like this, note group zero is always found in the match, for it is the whole actual match and not the broken out individual groups.

Groups: (0) (Key) (Value)

Match (1):
              0 : Key1:Value1;
            Key : Key1
          Value : Value1

Match (2):

              0 : Key2:value2;
            Key : Key2
          Value : value2

Match (3):

              0 : Key3;
            Key : Key3
          Value :

To see this regex in action see this forum post I answered that was marked as an answer which had the data spelled out.

Note instead of using indexes name the match groups by using Regex patern (?<ABC>  ) where ABC will name the match. Then you can access the matched value as such:

string myValue = m.Groups["ABC"].Value;