Thursday, August 6, 2015

Using named capture groups in regex C#

This will parse a string in the format of:
Key1:Value2
Key2:Value3
Key3:10:00:01 pm
Key4:Hello          

            Regex regex = new Regex(@"(?<key>\w+):(?<value>.*)\r\n");
            MatchCollection matchCollection = regex.Matches(input);
            foreach (Match match in matchCollection)
            {
                string key = match.Groups["key"].Value;
                string value = match.Groups["value"].Value;
            }

No comments:

Post a Comment

There was an error in this gadget