Click here to Skip to main content
15,912,400 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.6K   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 
It has been said over and over again in countless places that it is better to use an XML parser for doing things like this with (x)html documents.


here's a quote from a thread somewhere else about using RegExps for parsing XML (which is, essentially, what you're doing).

Some words of wisdom from Steve Ball:
"My characterisation of using REs for parsing XML is that it
is like performing brain surgery with a chainsaw: you get the
job done, but you have to scrape lots of important bits off
the wall and put them back in where they belong."
GeneralAlternative version Pin
Paw Jershauge27-Sep-10 12:55
Paw Jershauge27-Sep-10 12:55 

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.