-1

I would like to return all matches from the string and pattern below. My desired result would be 7 groups: e,f,e,g,e,e However, the console prints: e, e.

I can understand this method return only first match, so I get only e. But why is it printed twice? The letter e is repeated 4 times in a string.

string text = "hello from the regex project";
string pattern = "([e-g])";

System.Text.RegularExpressions.Regex r = new 
System.Text.RegularExpressions.Regex(pattern,RegexOptions.IgnoreCase);

Match m = r.Match(text);


foreach (var item in m.Groups)
{
    Console.WriteLine(item);
}

Console.ReadLine();
Rajeev Ranjan
  • 497
  • 4
  • 16
helpME1986
  • 933
  • 3
  • 12
  • 26

1 Answers1

0

You're only taking the first match, you need to work with all the matches something like:

    MatchCollection m = r.Matches(text);

    foreach (var item in m)
    {
        Console.WriteLine(item);
    }
BugFinder
  • 17,474
  • 4
  • 36
  • 51