public List<int> GetIds()
{
string html = "<p>hi <a class=\"test\" id=\"8369\">Anil Kumar</a> how are you? <a class=\"test\" id=\"8370\">Manoj Kumar</a></p>";
List<int> ids = new List<int>();
string pattern = "<a class=\"test\" id=\"(.*?)\">(.*?)</a>";
var rows = Regex.Matches(html, pattern, RegexOptions.Multiline);
if (rows.Count > 0)
{
foreach (Match row in rows)
{
int id = GetAttributeValue<int>(row.Value, "id");
ids.Add(id);
}
}
return ids;
}
public static T GetAttributeValue<T>(string htmlString, string attributeName)
{
string pattern = string.Format(@"(?<={0}="").*?(?="")", attributeName);
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
Match match = rgx.Match(htmlString);
return JsonConvert.DeserializeObject<T>(match.Value); //Method of Newtonsoft.Json
}
No comments:
Post a Comment