Posts

Showing posts from July, 2022

C# Regex matchall

  Reference See example of a "match all" in C#.   i   goes from 1 to 2 because there are two groups (two sets of parentheses specified in regex) using System; using System.Text.RegularExpressions; class Example { static void Main ( ) { string text = "One car red car blue car" ; string pat = @"(\w+)\s+(car)" ; // Instantiate the regular expression object. Regex r = new Regex(pat, RegexOptions.IgnoreCase); // Match the regular expression pattern against a text string. Match m = r.Match(text); int matchCount = 0 ; while (m.Success) { Console.WriteLine( "Match" + (++matchCount)); for ( int i = 1 ; i <= 2 ; i++) { Group g = m.Groups[i]; Console.WriteLine( "Group" +i+ "='" + g + "'" ); CaptureCollection cc = g.Captures; for ( int j = 0 ; j < cc.Count; j++