fork(1) download
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. class Example
  5. {
  6. static void Main()
  7. {
  8. string text = "start <p>I want to capture this</p> also <p>this</p> end";
  9. string pat = @"<p>(.+?)</p>";
  10.  
  11. // Instantiate the regular expression object.
  12. Regex r = new Regex(pat, RegexOptions.IgnoreCase);
  13.  
  14. // Match the regular expression pattern against a text string.
  15. Match m = r.Match(text);
  16. int matchCount = 0;
  17. while (m.Success)
  18. {
  19. Console.WriteLine("Match"+ (++matchCount));
  20. for (int i = 1; i <= 2; i++)
  21. {
  22. Group g = m.Groups[i];
  23. Console.WriteLine("Group"+i+"='" + g + "'");
  24. CaptureCollection cc = g.Captures;
  25. for (int j = 0; j < cc.Count; j++)
  26. {
  27. Capture c = cc[j];
  28. System.Console.WriteLine("Capture"+j+"='" + c + "', Position="+c.Index);
  29. }
  30. }
  31. m = m.NextMatch();
  32. }
  33. }
  34. }
  35.  
Success #stdin #stdout 0.06s 18928KB
stdin
Standard input is empty
stdout
Match1
Group1='I want to capture this'
Capture0='I want to capture this', Position=9
Group2=''
Match2
Group1='this'
Capture0='this', Position=44
Group2=''