Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- country">([w]*)<{.*n.*n.*n.*"city">([w]*)}
- string HTML = @"
- <table style='width:100%'>
- <tr><td class='country'>Germany</td></tr>
- <tr><td class='city'>Berlin</td></tr>
- <tr><td class='city'>Cologne</td></tr>
- <tr><td class='city'>Munich</td></tr>
- <tr><td class='country'>France</td></tr>
- <tr><td class='city'>Paris</td></tr>
- <tr><td class='country'>USA</td></tr>
- <tr><td class='city'>New York</td></tr>
- <tr><td class='city'>Las Vegas</td></tr>
- </table>";
- var regex = new Regex(
- @"
- class=[^>]*?
- (?<class>[-wd_]+)
- [^>]*>
- (?<text>[^<]+)
- <
- ",
- RegexOptions.Compiled | RegexOptions.IgnoreCase
- | RegexOptions.IgnorePatternWhitespace
- );
- var country = string.Empty;
- var Countries = new Dictionary<string, List<string>>();
- foreach (Match match in regex.Matches(HTML))
- {
- string countryCity = match.Groups["class"].Value.Trim();
- string text = match.Groups["text"].Value.Trim();
- if (countryCity.Equals("country", StringComparison.OrdinalIgnoreCase))
- {
- country = text;
- Countries.Add(text, new List<string>());
- }
- else
- {
- Countries[country].Add(text);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement