c# - Extract data from string using pattern -
hi have long string:
'bla bla bla... <img src="/uploads/photo.png" width="143" height="136" /> bla bla bla...'
and want extract long string: image tag - width, height , of course path... maybe this:
<img src="*" width="*" height="*" />
but don't know how "*" data. can please suggest me code extract image path (*
). might in c#, vb or java... anything. thanks!
don't use regex parse html. use html parser insted. e.g. can use htmlagilitypack:
var html = "bla... <img src=\"/uploads/photo.png\" width=\"143\" height=\"136\" /> bla..."; htmldocument doc = new htmldocument(); doc.loadhtml(html); var img = doc.documentnode.element("img"); var src = img.attributes["src"].value; // "/uploads/photo.png" var width = img.attributes["width"].value; // "143" var height = img.attributes["height"].value; // "136"
or anglesharp
var parser = new htmlparser(); var doc = parser.parse(html); var img = doc.queryselectorall("img").firstordefault(); var src = img.attributes["src"].value; // "/uploads/photo.png" var width = img.attributes["width"].value; // "143" var height = img.attributes["height"].value; // "136"
Comments
Post a Comment