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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
Instead of using Regex on HTML, consider using the Html Agility Pack instead.
Method 3
The following regex does the trick:
href=['"]([^'"]+?)['"]
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0