fork download
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. class Example
  5. {
  6. static void match(string pat, string text)
  7. {
  8. // Instantiate the regular expression object.
  9. Regex r = new Regex(pat, RegexOptions.IgnoreCase);
  10.  
  11. // Match the regular expression pattern against a text string.
  12. Match m = r.Match(text);
  13. int matchCount = 0;
  14. while (m.Success)
  15. {
  16. Console.WriteLine("Match"+ (++matchCount));
  17. for (int i = 1; i <= 3; i++)
  18. {
  19. Group g = m.Groups[i];
  20. Console.WriteLine("Group" + i + " = '" + g + "'");
  21. }
  22. m = m.NextMatch();
  23. }
  24. }
  25. static void Main()
  26. {
  27. string pat = @"(?:(<div id[^>]+>)(\w+))?([\ _]{3,})";
  28. match(pat, "<div id=\"div1\">Street ___________________ </div>");
  29. match(pat, "<div id=\"div2\">CAP |__|__|__|__|__| number ______ </div>");
  30. match(pat, "<div id=\"div3\">City _____________________ State |__|__|</div>");
  31. match(pat, "<div id=\"div4\">City2 ____________________ State2 _____</div>");
  32. }
  33. }
Success #stdin #stdout 0.07s 34216KB
stdin
Standard input is empty
stdout
Match1
Group1 = '<div id="div1">'
Group2 = 'Street'
Group3 = ' ___________________ '
Match1
Group1 = ''
Group2 = ''
Group3 = ' ______ '
Match1
Group1 = '<div id="div3">'
Group2 = 'City'
Group3 = ' _____________________ '
Match1
Group1 = '<div id="div4">'
Group2 = 'City2'
Group3 = ' ____________________ '
Match2
Group1 = ''
Group2 = ''
Group3 = ' _____'