0

I try to make a simple regex with Rubular

I want to capture all the url (src) with img id "zoom-product-image" where img_b.jpg match.

I've tried with this regex

/ id="zoom-product-image" src="(.*?)"

This will leave me with 3 matches.

/product-images/545442_img_a.jpg

/product-images/5453112_img_b.jpg

/product-images/595512_img_c.jpg

But i need only match witch containing img_b.jpg so I've tried to use this regex:

/ id="zoom-product-image" src="(.*?_img_b.jpg)"

But this does not work

HTML test string:

<li class=""><img id="zoom-product-image" src="/product-images/545442_img_a.jpg" alt="Red img"> </li><li><img id="zoom-product-image" src="/product-images/5453112_img_b.jpg" alt="Green img"></li><li><img id="zoom-product-image" src="/product-images/595512_img_c.jpg" alt="Blue img"></li>
StvnBrkdll
  • 3,506
  • 1
  • 20
  • 28

2 Answers2

1

Probably not the best solution, but it works

/id="zoom-product-image" src="([^"]*_img_b\.jpg)"/g

[^"]* means - anything but " 0 or more times

Rai
  • 609
  • 6
  • 14
  • 1
    To match literal `.` needs to be escaped `\.` from it's [special meaning](http://www.regular-expressions.info/dot.html) – bobble bubble Nov 13 '16 at 14:37
1

This appears to meet your requirements:

 id="zoom-product-image" src="([^_]*_img_b\.jpg)

Breaking it down:

  • id="zoom-product-image" src=" : match everything beginning with this string
  • ( : begin capture
  • [^_]* : match 0 or more characters that are NOT _
  • _img_b.jpg : match this string
  • ) : end capture
StvnBrkdll
  • 3,506
  • 1
  • 20
  • 28