Regex to get src value from an img tag

Parse your HTML with something else. HTML is not regular and thus regular expressions aren’t at all suited to parsing it.

Use an HTML parser, or an XML parser if the HTML is strict. It’s a lot easier to get the src attribute’s value using XPath:

//img/@src

XML parsing is built into the System.Xml namespace. It’s incredibly powerful. HTML parsing is a bit more difficult if the HTML isn’t strict, but there are lots of libraries around that will do it for you.

Leave a Comment