I’m editing this function code and wondering how to implement a count function to return, along with the index and match, to writeline. Some things I’ve considered, making a LinkedList<MatchCounter> matchCounters = new LinkedList<MatchCounter>();
and passing matches into with .addlast
and calling a method increment();
but I can’t get over the conversion types from regex to strings. Or calling the count method like int owTally = match.Groups["ow"].Captures.Count;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; namespace ConsoleApplication10 { class Program { static void Main(string[] args) { LinkedList<MatchCounter> matchCounters = new LinkedList<MatchCounter>(); var patterns = new[] { "ow", "ace", "dis", "ore", "rab", "rent", "com" }; var pa = new StringBuilder(); for (var i = 0; i < patterns.Length; i++) pa.Append(@"(?=(?<p").Append(i).Append(">").Append(patterns[i]).Append("))?"); var regexx = new Regex(pa.ToString(), RegexOptions.Compiled); Console.WriteLine("pattern: {0}", regexx); var input = Console.ReadLine(); ; Console.WriteLine("input: {0} ", input); foreach (var match in regexx.Matches(input).Cast<Match>()) { for (var i = 0; i < patterns.Length; i++) { var group = match.Groups["p" + i]; if (!group.Success) continue; Console.WriteLine("Matched pattern #{0}: '{1}' at index {2}", i, group.Value, group.Index); } //matchCounters.AddLast(new MatchCounter(match)); } Console.ReadKey(); } } public class MatchCounter { int count; string syllable; public MatchCounter(String Syl) { count = 1; syllable = Syl; } public string GetSyllable() { return syllable; } public void Increment() { count++; } public int GetCount() { return count; } }
}