RegEx Group : Regex Group « Regular Expression « C# / CSharp Tutorial






using System;
using System.Text.RegularExpressions;

    class Test
    {
        public static void Main()
        {
            string string1 = "04:03:27 127.0.0.0 yourdomain.com";
            Regex theReg = new Regex(@"(?<time>(\d|\:)+)\s" +
                @"(?<ip>(\d|\.)+)\s" +
                @"(?<site>\S+)");
            MatchCollection theMatches = theReg.Matches(string1);
            foreach (Match theMatch in theMatches)
            {
                if (theMatch.Length != 0)
                {
                    Console.WriteLine("\ntheMatch: {0}",theMatch.ToString());
                    Console.WriteLine("time: {0}",theMatch.Groups["time"]);
                    Console.WriteLine("ip: {0}",theMatch.Groups["ip"]);
                    Console.WriteLine("site: {0}",theMatch.Groups["site"]);
                }
            }
        }
    }








17.4.Regex Group
17.4.1.Match Group Value
17.4.2.Get Group in a match
17.4.3.Name a Regex group
17.4.4.RegEx Group
17.4.5.Get named group from Match