fork(1) download
  1. using System;
  2. using System.Text.RegularExpressions;
  3. class Program {
  4. static void Main() {
  5. string ourString = @"one-two-three:uno-dos-tres one-two-three:ichi-ni-san";
  6. string ourPattern = @"(?:(?<someword>\w+)-?)+:(?:(?<someword>\w+)-?)+";
  7. var ourRegex = new Regex(ourPattern);
  8. MatchCollection AllMatches = ourRegex.Matches(ourString);
  9.  
  10. Console.WriteLine("**** Understanding Named Capture Reuse ****");
  11. Console.WriteLine("\nOur string today is:" + ourString);
  12. Console.WriteLine("Our regex today is:" + ourPattern);
  13. Console.WriteLine("Number of Matches: " + AllMatches.Count);
  14.  
  15. Console.WriteLine("\n*** Let's Iterate Through the Matches ***");
  16. int matchNum = 1;
  17. foreach (Match SomeMatch in AllMatches) {
  18. Console.WriteLine("Match number: " + matchNum++);
  19. Console.WriteLine("Overall Match: " + SomeMatch.Value);
  20. Console.WriteLine("\nNumber of Groups: " + SomeMatch.Groups.Count);
  21. Console.WriteLine("Why two Groups, not one? Because the overall match is Group 0");
  22. // Another way of printing the overall match
  23. Console.WriteLine("Groups[0].Value = " + SomeMatch.Groups[0].Value);
  24. Console.WriteLine(@"Since the 'someword' group appears more than once in the pattern, the value of Groups[1] and Groups[""someword""] is the last capture of each group");
  25. Console.WriteLine("Groups[1].Value = " + SomeMatch.Groups[1].Value);
  26. Console.WriteLine(@"Groups[""someword""].Value = " + SomeMatch.Groups["someword"].Value);
  27. // Let's look all the first captures manually
  28. Console.WriteLine("Someword Capture 0: " + SomeMatch.Groups["someword"].Captures[0].Value);
  29.  
  30. // Let's look at someword captures automatically
  31. int somewordCapCount = SomeMatch.Groups["someword"].Captures.Count;
  32. Console.WriteLine("Number of someword Captures: " + somewordCapCount);
  33. int i2 = 0;
  34. foreach (Capture someword in SomeMatch.Groups["someword"].Captures) {
  35. Console.WriteLine("someword Capture " + i2 + ": " + someword.Value);
  36. i2++;
  37. } // end iterate G2 captures
  38. Console.WriteLine("\n");
  39. } // end iterate matches
  40.  
  41. Console.WriteLine("\nPress Any Key to Exit.");
  42. Console.ReadKey();
  43.  
  44. } // END Main
  45. } // END Program
Success #stdin #stdout 0.07s 34152KB
stdin
Standard input is empty
stdout
**** Understanding Named Capture Reuse ****

Our string today is:one-two-three:uno-dos-tres one-two-three:ichi-ni-san
Our regex today is:(?:(?<someword>\w+)-?)+:(?:(?<someword>\w+)-?)+
Number of Matches: 2

*** Let's Iterate Through the Matches ***
Match number: 1
Overall Match: one-two-three:uno-dos-tres

Number of Groups: 2
Why two Groups, not one? Because the overall match is Group 0
Groups[0].Value = one-two-three:uno-dos-tres
Since the 'someword' group appears more than once in the pattern, the value of Groups[1] and Groups["someword"] is the last capture of each group
Groups[1].Value = tres
Groups["someword"].Value = tres
Someword Capture 0: one
Number of someword Captures: 6
someword Capture 0: one
someword Capture 1: two
someword Capture 2: three
someword Capture 3: uno
someword Capture 4: dos
someword Capture 5: tres


Match number: 2
Overall Match: one-two-three:ichi-ni-san

Number of Groups: 2
Why two Groups, not one? Because the overall match is Group 0
Groups[0].Value = one-two-three:ichi-ni-san
Since the 'someword' group appears more than once in the pattern, the value of Groups[1] and Groups["someword"] is the last capture of each group
Groups[1].Value = san
Groups["someword"].Value = san
Someword Capture 0: one
Number of someword Captures: 6
someword Capture 0: one
someword Capture 1: two
someword Capture 2: three
someword Capture 3: ichi
someword Capture 4: ni
someword Capture 5: san



Press Any Key to Exit.