-2

I m new on this field so i don't have much idea about regex. My problem is like this I have a "div" within it their is a "href" attribute and finally their is "img". I want a regex to match the "img" source content by which i can get the data of that content if the url of that particular content matches.

<div class="test_list3">
   <a href="/photo/tst_t_45288/6782/12/1/">`<img  src="http://example.com/1700/tst_t_45288/m2/tst_t_45288_1.jpg" title="tst-t  1" alt="tst-t 1" /></a><br /><a href="http://example.com/1700/tst_t_45288/tst_t_45288_1.jpg"  target="_blank">jpg file</a>
</div>

Any suggestions ?

DreadPirateShawn
  • 8,164
  • 4
  • 49
  • 71
Uraniium
  • 109
  • 6

2 Answers2

0

If using a regex is your last choice, you can go with this:

(?s)<div[^>]*>.*<img.*src=\"([^"]+)\".*<\/div>

Live demo

Explanation

  • because you have multiple lines s flag needed to make dot matches newlines.
  • then checking for opening div tag and all its attributes by <div[^>]*>
    • [^>]* zero or more characters but not >
  • then we should match all other things till we reach to opening img tag by .*
  • after that we should go through possibly attributes or spaces before src attribute here <img.*src
  • then capturing all characters except " in src : src=\"([^"]+)
  • and finally escape all things till first occurrence of closing div tag happens
revo
  • 47,783
  • 14
  • 74
  • 117
0

I think I cannot do this by only one reg exp

1.Using <div[^>]*>[^<>]*(((?'Open'<div[^>]*>)[^<>]*)+((?'-Open'</div>)[^<>]*)+)*(?(Open)(?!))</div> get the content between two div tags.

2.Using other reg exp matchs the matching string in step1 and get the content between the img tags.