Sahil Sharma
Sahil Sharma

Reputation: 4247

Regex to validate image urls with query string

I want to validate image urls, few with query string and few without it. https://images.company.com/img/800X450/vehicle/201610/858823_589_1475572549658.gif?bg=000000&wmi=n

http://images.company.com/img/800X450/vehicle/201610/858823_599_1475572549658.jpeg?bg=000000&wmi=n&pne=ur

http://images.company.com/img/800X450/vehicle/201610/858823_599_1475572549658.png

So basic thing is to validate any image with jpg|png|gif|jpeg extension, with or without query string parameter.

I tried using Matches(@".(?i)(jpg|png|gif|jpeg)") regex.

But problem is it is validating images like: http://images.company.com/img/800X450/vehicle/201610/858823_599_1475572549658.pngg OR http://images.company.com/img/800X450/vehicle/201610/858823_599_1475572549658.giff

How can i validate if url has exact extension jpg or png or gif or jpeg and optionally some query string parameter after that?

Upvotes: 1

Views: 1864

Answers (1)

sideroxylon
sideroxylon

Reputation: 4416

Assert that the listed file extension must be followed by a query string or end of string:

(jpg|jpeg|gif|png)((\?+*)?$|$)

HERE

Upvotes: 0

Related Questions