Home > Enterprise >  Regex matches only last occurence instead of all occurrences
Regex matches only last occurence instead of all occurrences

Time:10-14

I have this pattern

.*;\/wp-content\/uploads\/(.*)\.

and this in string format

<div  data-images="{&#34;omniture&#34;:{&#34;type&#34;:&#34;&#34;,&#34;hasImpression&#34;:false},&#34;slides&#34;:[{&#34;title&#34;:&#34;&#34;,&#34;omniture&#34;:null,&#34;description&#34;:&#34;&#34;,&#34;path&#34;:&#34;/wp-content/uploads/auto-shows/frankfurt/2017/2019-porsche-cayenne/02-2019-porsche-cayenne-turbo-NP.jpg&#34;,&#34;thumbnail&#34;:&#34;/wp-content/uploads/auto-shows/frankfurt/2017/2019-porsche-cayenne/02-2019-porsche-cayenne-turbo-NP.jpg?w=320&h=240&crop=1&#34;},{&#34;title&#34;:&#34;&#34;,&#34;omniture&#34;:null,&#34;description&#34;:&#34;&#34;,&#34;path&#34;:&#34;/wp-content/uploads/make/porsche/cayenne/2019/oem/21-2019-porsche-cayenne.jpg&#34;,&#34;thumbnail&#34;:&#34;/wp-content/uploads/make/porsche/cayenne/2019/oem/21-2019-porsche-cayenne.jpg?w=320&h=240&crop=1&#34;},{&#34;title&#34;:&#34;&#34;,&#34;omniture&#34;:null,&#34;description&#34;:&#34;&#34;,&#34;path&#34;:&#34;/wp-content/uploads/make/porsche/cayenne/2019/oem/23-2019-porsche-cayenne.jpg&#34;,&#34;thumbnail&#34;:&#34;/wp-content/uploads/make/porsche/cayenne/2019/oem/23-2019-porsche-cayenne.jpg?w=320&h=240&crop=1&#34;},{&#34;title&#34;:&#34;&#34;,&#34;omniture&#34;:null,&#34;description&#34;:&#34;&#34;,&#34;path&#34;:&#34;/wp-content/uploads/make/porsche/cayenne/2019/oem/30-2019-porsche-cayenne.jpg&#34;,&#34;thumbnail&#34;:&#34;/wp-content/uploads/make/porsche/cayenne/2019/oem/30-2019-porsche-cayenne.jpg?w=320&h=240&crop=1&#34;}]}"></div>

I want to match all the occurrences that appear between ;/wp-content/uploads/ and .jpg

I tried to match this, but it shows last group only.

I am new to regex, so please correct me with the implementation here, thank you.

CodePudding user response:

You can make use of lookarounds to do clean extractions:

(?<=;\/wp-content\/uploads\/)(.*?)(?=\.jpg)

https://regex101.com/r/vzJ0FU/1

  • Related