1

I have a string in which I have anchor tag I want to know the href values of those anchor tags. my string is like:

This is Test page <a href='test.aspx'>test page</a> .

in this I want to find the value of href i.e. test.aspx

Please suggest me any good regx group for this.

Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43
Askiitians
  • 291
  • 7
  • 18

3 Answers3

4

if you use <a [^>]*href=(?:'(?<href>.*?)')|(?:"(?<href>.*?)") then the result will be stored in the named group href

Example:

var inputString="This is Test page <a href='test.aspx'>test page</a>";
var regex=new Regex("<a [^>]*href=(?:'(?<href>.*?)')|(?:\"(?<href>.*?)\")",RegexOptions.IgnoreCase);
var urls=regex.Matches(inputString).OfType<Match>().Select(m =>m.Groups["href"].Value);

urls will be a collection of strings containing the hrefs.

Bob Vale
  • 18,094
  • 1
  • 42
  • 49
3

The following regex does the trick:

href=['"]([^'"]+?)['"]
Lea Hayes
  • 62,536
  • 16
  • 62
  • 111
2

Instead of using Regex on HTML, consider using the Html Agility Pack instead.

Mark Maslar
  • 1,121
  • 4
  • 16
  • 28