Regex to search for everything before a string, and after

171 views Asked by At

I would like to search for everything before the string http://thepaperwall.com/wallpapers/ but not including that string. I would also like to search for everything after .jpg but not including .jpg.

So my final string should look like this:

http://thepaperwall.com/wallpapers/cityscape/small/small_642331afcd78d0840485bb352d99b289b50e8467.jpg

How can I do this?

2

There are 2 answers

8
Szymon On

You can use look-behind and look-ahead to get a string position between 2 other strings:

(?<=http://thepaperwall.com/wallpapers/).*(?=\.jpg)
0
revo On

You need a non-capturing group:

(.*)(?:http:\/\/thepaperwall\.com\/wallpapers\/.*\.jpg)(.*)

Live demo