Regular expression to find image url in <img> tag in HTML using VB.Net code
3 answers
I would recommend using an HTML parser to read the html and pull the image tags out of it, as regexes don't converge well with data structures like xml and html.
In C #: ( from this SO question)
var web = new HtmlWeb();
var doc = web.Load("http://www.stackoverflow.com");
var nodes = doc.DocumentNode.SelectNodes("//img[@src]");
foreach (var node in nodes)
{
Console.WriteLine(node.src);
}
+4
a source to share
Try it *:
<img .*?src=["']?([^'">]+)["']?.*?>
Tested here with:
<img class="test" src="/content/img/so/logo.png" alt="logo homepage">
gives
$1 = /content/img/so/logo.png
$ 1 (you have to hover over it to see it) matches the part of the regular expression between (). How you access this value will depend on which regex implementation you are using.
* If you want to know how it works, please leave a comment
EDIT As always with regex, there are cases of cross:
<img title="src=hack" src="/content/img/so/logo.png" alt="logo homepage">
This will correspond to a "hack".
+2
a source to share