c# - How to grab URL from string with specified alt tags -


i extract "http://www.somewebsite.com/wanted.jpg" string below alt set "thumbnail", , avoid grabbing http://www.somewebsite.com/notwanted.jpg :

<span>some information here   <div>      <img src="http://www.somewebsite.com/notwanted.jpg" width="15" height="15" alt="emoticon">      <img src="http://www.somewebsite.com/wanted.jpg" alt="thumbnail">    </div> </span> 

what easiest way that?

with warnings parsing html regex, c# regex match url want:

(?<=src=")[^"]+(?="[^">]*?alt="thumbnail") 

see demo.

to test in c#:

var myregex = new regex("(?<=src=\")[^\"]+(?=\"[^\">]*?alt=\"thumbnail\")"); string resultstring = myregex.match(s1).value; console.writeline(resultstring); 

output:

http://www.somewebsite.com/wanted.jpg

explanation

  • the lookbehind (?<=src=") asserts precedes src="
  • [^"]+ matches chars not " (that's want)
  • the lookahead (?="[^">]*?alt="thumbnail")asserts follows quote, , chars not quote or > followed `alt="thumbnail"

reference


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -