Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. country">([w]*)<{.*n.*n.*n.*"city">([w]*)}
  2.  
  3. string HTML = @"
  4. <table style='width:100%'>
  5. <tr><td class='country'>Germany</td></tr>
  6. <tr><td class='city'>Berlin</td></tr>
  7. <tr><td class='city'>Cologne</td></tr>
  8. <tr><td class='city'>Munich</td></tr>
  9. <tr><td class='country'>France</td></tr>
  10. <tr><td class='city'>Paris</td></tr>
  11. <tr><td class='country'>USA</td></tr>
  12. <tr><td class='city'>New York</td></tr>
  13. <tr><td class='city'>Las Vegas</td></tr>
  14. </table>";
  15.  
  16. var regex = new Regex(
  17. @"
  18. class=[^>]*?
  19. (?<class>[-wd_]+)
  20. [^>]*>
  21. (?<text>[^<]+)
  22. <
  23. ",
  24. RegexOptions.Compiled | RegexOptions.IgnoreCase
  25. | RegexOptions.IgnorePatternWhitespace
  26. );
  27.  
  28. var country = string.Empty;
  29. var Countries = new Dictionary<string, List<string>>();
  30. foreach (Match match in regex.Matches(HTML))
  31. {
  32. string countryCity = match.Groups["class"].Value.Trim();
  33. string text = match.Groups["text"].Value.Trim();
  34. if (countryCity.Equals("country", StringComparison.OrdinalIgnoreCase))
  35. {
  36. country = text;
  37. Countries.Add(text, new List<string>());
  38. }
  39. else
  40. {
  41. Countries[country].Add(text);
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement