alphaexe
alphaexe

Reputation: 25

Regular expression on a URL

I need to be able to catch when the URL contains a image of multiple file types or follow this syntax.

http://localhost:8080/fdlic-web/webpic/101

Here is what i have so far.

(.*)(jpg|gif|png|bmp|jpeg|webpic/(\d+))$

Upvotes: 1

Views: 206

Answers (1)

Tomalak
Tomalak

Reputation: 338406

Good so far, just use less grouping and kick out the .*, you don't need it.

(?:webpic/\d+|gif|png|bmp|jpe?g)$

Actually, it's less ambiguous to delimit your directories/file types to prevent partial matches that yield false positives:

(?:/(?:webpic/\d+)|\.(?:gif|png|bmp|jpe?g))$
    ^                             ^
    | all path components here    |
         all file extensions here |

Upvotes: 2

Related Questions