fork(3) download
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. class Program {
  5. static void Main() {
  6. string ourString = @"Italian:one=uno,two=due Japanese:one=ichi,two=ni,three=san";
  7. string ourPattern = @"\w+:(?:(\w+)=(\w+),?)+";
  8. var ourRegex = new Regex(ourPattern);
  9. MatchCollection AllMatches = ourRegex.Matches(ourString);
  10.  
  11. Console.WriteLine("**** Understanding .NET Capture Groups with Quantifiers ****");
  12. Console.WriteLine("\nOur string today is:" + ourString);
  13. Console.WriteLine("Our regex today is:" + ourPattern);
  14. Console.WriteLine("Number of Matches: " + AllMatches.Count);
  15.  
  16. Console.WriteLine("\n*** Let's Iterate Through the Matches ***");
  17. int matchNum = 1;
  18. foreach (Match SomeMatch in AllMatches) {
  19. Console.WriteLine("Match number: " + matchNum++);
  20. Console.WriteLine("Overall Match: " + SomeMatch.Value);
  21. Console.WriteLine("\nNumber of Groups: " + SomeMatch.Groups.Count);
  22. Console.WriteLine("Why three Groups, not two? Because the overall match is Group 0");
  23. // Another way of printing the overall match
  24. Console.WriteLine("Group 0: " + SomeMatch.Groups[0].Value);
  25. Console.WriteLine("Since Groups 1 and 2 have quantifiers, the value of Group 1 and Group 2 is the last capture of each group");
  26. Console.WriteLine("Group 1: " + SomeMatch.Groups[1].Value);
  27. Console.WriteLine("Group 2: " + SomeMatch.Groups[2].Value);
  28. // For this match, let's look all the Group 1 captures manually
  29. int g1capCount = SomeMatch.Groups[1].Captures.Count;
  30. Console.WriteLine("Number of Group 1 Captures: " + g1capCount);
  31. Console.WriteLine("Group 1 Capture 0: " + SomeMatch.Groups[1].Captures[0].Value);
  32. Console.WriteLine("Group 1 Capture 1: " + SomeMatch.Groups[1].Captures[1].Value);
  33. // To be safe, we'll check if we have a third capture for Group 1
  34. // Because the first overall match only has two captures
  35. if(g1capCount>2) Console.WriteLine("Group 1 Capture 2: " + SomeMatch.Groups[1].Captures[2].Value);
  36. // Let's look at Group 2 captures automatically
  37. int g2capCount = SomeMatch.Groups[2].Captures.Count;
  38. Console.WriteLine("Number of Group 2 Captures: " + g2capCount);
  39. int i2 = 0;
  40. foreach (Capture g2capture in SomeMatch.Groups[2].Captures ) {
  41. Console.WriteLine("Group 2 Capture " + i2 + ": " + g2capture.Value);
  42. i2++;
  43. } // end iterate G2 captures
  44. Console.WriteLine("\n");
  45. } // end iterate matches
  46.  
  47. Console.WriteLine("\nPress Any Key to Exit.");
  48. Console.ReadKey();
  49.  
  50. } // END Main
  51. } // END Program
Success #stdin #stdout 0.08s 34128KB
stdin
Standard input is empty
stdout
**** Understanding .NET Capture Groups with Quantifiers ****

Our string today is:Italian:one=uno,two=due Japanese:one=ichi,two=ni,three=san
Our regex today is:\w+:(?:(\w+)=(\w+),?)+
Number of Matches: 2

*** Let's Iterate Through the Matches ***
Match number: 1
Overall Match: Italian:one=uno,two=due

Number of Groups: 3
Why three Groups, not two? Because the overall match is Group 0
Group 0: Italian:one=uno,two=due
Since Groups 1 and 2 have quantifiers, the value of Group 1 and Group 2 is the last capture of each group
Group 1: two
Group 2: due
Number of Group 1 Captures: 2
Group 1 Capture 0: one
Group 1 Capture 1: two
Number of Group 2 Captures: 2
Group 2 Capture 0: uno
Group 2 Capture 1: due


Match number: 2
Overall Match: Japanese:one=ichi,two=ni,three=san

Number of Groups: 3
Why three Groups, not two? Because the overall match is Group 0
Group 0: Japanese:one=ichi,two=ni,three=san
Since Groups 1 and 2 have quantifiers, the value of Group 1 and Group 2 is the last capture of each group
Group 1: three
Group 2: san
Number of Group 1 Captures: 3
Group 1 Capture 0: one
Group 1 Capture 1: two
Group 1 Capture 2: three
Number of Group 2 Captures: 3
Group 2 Capture 0: ichi
Group 2 Capture 1: ni
Group 2 Capture 2: san



Press Any Key to Exit.