Capture Collection : Regex Match « Regular Expression « C# / CSharp Tutorial






using System;
using System.Text.RegularExpressions;

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








17.6.Regex Match
17.6.1.Match words that start with 's'
17.6.2.Match words that start with 's' and end with 'e'
17.6.3.Match words that contain two consecutive identical characters
17.6.4.Match words that contain 'u'
17.6.5.Match words that contain the pattern 'ai'
17.6.6.Match words that contain the pattern 'ai' or 'ie'
17.6.7.Match words that contain 'k' or 'f'
17.6.8.Match words that contain any letters in the range 'b' through 'd'
17.6.9.Use Regex to validate your input
17.6.10.Get matched parts
17.6.11.Match index and value
17.6.12.Next Match
17.6.13.Using Match Collection
17.6.14.Capture Collection
17.6.15.\s Match a white-space character.
17.6.16.[A-Z] Match any uppercase character from A to Z.
17.6.17.\w* Match zero or more word characters.
17.6.18.\b Match a word boundary.
17.6.19.th Match the literal characters "th".
17.6.20.[^o] Match any character that is not an "o".
17.6.21.\w+ Match one or more word characters.
17.6.22.Strip HTML start and end tags from each string if they are present