Click here to Skip to main content
15,899,314 members
Articles / All Topics

Regular Expression for Image Source

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
28 Sep 2010CPOL1 min read 15.5K   4   3
A Regular Expression to get the source value of an image tag regardless of the location of the src attribute

Here is a Regular Expression to get the source value of an image tag regardless of the location of the src attribute.

(?<=img\s+[^>]*src\=[\x27\x22])[^\x27\x22]*(?=[\x27\x22])

Options: case insensitive

  • Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=img\s+[^>]*src\=[\x27\x22])»
    • Match the characters “img” literally «img»
    • Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) «\s+»
      • Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
    • Match any character that is NOT a “>«[^>]*»
      • Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    • Match the characters “src” literally «src»
    • Match the character “=” literally «\=»
    • Match a single character present in the list below «[\x27\x22]»
    • ASCII character 0×27 (39 decimal) «\x27»
    • ASCII character 0×22 (34 decimal) «\x22»
  • Match a single character NOT present in the list below «[^\x27\x22]*»
    • Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    • ASCII character 0×27 (39 decimal) «\x27»
    • ASCII character 0×22 (34 decimal) «\x22»
  • Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=[\x27\x22])»
    • Match a single character present in the list below «[\x27\x22]»
      • ASCII character 0×27 (39 decimal) «\x27»
      • ASCII character 0×22 (34 decimal) «\x22»

Created with RegexBuddy

View my technical blog entries on .

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) None
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Richard Waddell7-Oct-10 16:08
Richard Waddell7-Oct-10 16:08 
General[My vote of 1] an XML Parser would be better suited Pin
hahanottelling5-Oct-10 19:48
hahanottelling5-Oct-10 19:48 
GeneralAlternative version Pin
Paw Jershauge27-Sep-10 12:55
Paw Jershauge27-Sep-10 12:55 
WebBrowser wb = new WebBrowser();
wb.DocumentText = "<IMG Name=\"myfirstimage\" src=\"http://link.to.my.first.image.gif\"><br><IMG Name=\"mysecondimage\" SRC=\"http://link.to.my.second.image.gif\">";
do { Application.DoEvents(); }
while (wb.ReadyState != WebBrowserReadyState.Complete);
HtmlDocument hd = wb.Document;
foreach (HtmlElement he in hd.Images)
    Debug.WriteLine(he.GetAttribute("name") + " = " + he.GetAttribute("src"));

With great code, comes great complexity, so keep it simple stupid...Shucks | :-\ Shucks | :-\

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.