-8

can someone please help me for below

I have a string which stores html, need to find the anchor tag and get the value of href of that tag using C#

For Example:

<p>can someone please help me for below </p>
<p>for Link <a href="www.ffb.cc">Click Here</a></p>

I need to get the href value ..generic code appreciated

Edit:

Tried <a\s+(?:[^>]*?\s+)?href="([^"]*)"

but no matches found

Note: I am unable to post the pic due to Reputation points :(

USER1287
  • 53
  • 5

1 Answers1

3

Don't use Regex to parse html. A correct way would be using an html parser like HmlAgilityPack

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlstring);

var links = doc.DocumentNode.SelectNodes("//a")
               .Select(a => a.Attributes["href"].Value)
               .ToList();
Community
  • 1
  • 1
EZI
  • 15,209
  • 2
  • 27
  • 33