tags:

views:

88

answers:

2

I am getting URLs like this:

http://img23.example.com/images/j43j32k3.jpg

And I need to identify whether it both:

a) Begins with HTTP

b) Ends with JPEG, JPG, GIF or PNG

I am using Ruby to do the matching I just don't know the Regex for this one..

+1  A: 

The following regex delimited by /'s matches a string beginning with htpp and ending with one of the image extensions specified in your question

/\Ahttp.*(jpeg|jpg|gif|png)\Z/

ennuikiller
OP, can image urls have GET params (in which case, this regex needs to be more complex)? Are you always performing a match on a single URL (if not, this regex needs to be non-greedy .*?, not .*)? Do you need to catch certain scripts that generate images, like gen.php?name=pic1, for example?
Artem Russakovskii
Also, you need to enable case-insensitivity with the i flag.
Artem Russakovskii
I am looking to analyze 1 single URL at a time, for an absolute link to an image.
dMix
The regex with the i flag (for case insensitivity as mentioned by Adam) matches your requirements as specified. If your requirement includes something not specified then please indicate it. Thanks!
ennuikiller
+1  A: 

%r{^http://.*\.(jpeg|jpg|gif|png)$}i

Daniel Aborg
This has the benefit of checking for http:// to make sure it's really a URL, and that it ends with e.g. .gif to make sure it's really an extension -- plus it is case-insensitive so it matches things like .GIF etc.
Daniel Aborg