How to get href URL from HTML anchor tags using RegEx

This will capture the URL as well as the text.

string input = @"<a href=""\page1.htm"">Page 1</a><a href=""\page2.htm"">Page 2</a>";
				
var matches = Regex.Matches(input, @"<a\shref=""(?<url>.*?)"">(?<text>.*?)</a>");

foreach (Match m in matches)
{
	Console.WriteLine("URL: " + m.Groups["url"].Value + "; Text: " + m.Groups["text"].Value);
}

Comments

Leave a Comment

All fields are required. Your email address will not be published.