3

I have a HTML string and I want to find image links like <a href="link1"><img src="link2"/></a>

I try this regex pattern but it's not working: "\<a\s.*\<img.*\<\/a\>"

When I use that pattern it finds a string like <a href="link1">some text1</a>some text2.<a href="link1"><img src="link2"/></a> but I don't want first <a href part. I just want img tags inside a tag and I need help about it.

Dogukan Demir
  • 121
  • 2
  • 12
  • Why not use an HTML parser instead? – fge May 26 '13 at 15:45
  • I would say use HTML parser (like [JSoup](http://jsoup.org/)) [instead of regex](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). But for this simple case maybe just use groups. – Pshemo May 26 '13 at 15:46

1 Answers1

0

When you want only part of your overall regex you need to use groups or back references( ).

"\<a\s.*\(<img.*\/>)<\/a\>"

Then depending on the language you need to get the first group.

http://www.regular-expressions.info/brackets.html

Update: To match exactly try:

 \<a[^<]*\<img.*\<\/a\>

instead of a \s I used [^<] which is any character except a <.

Lance Hudson
  • 151
  • 7
  • This will not work for me because I want not just something inside img tags. I want img tags inside a tag together. I will cover that string with a html code with some css and publish it to my service. I need exactly a string which is `` – Dogukan Demir May 26 '13 at 15:57
  • added update that should match only the inner . You may need to adjust to handle spaces or other differences. – Lance Hudson May 26 '13 at 16:06