user886658
user886658

Reputation: 19

How to match one number in url with regex

I have this URL and I only want to match 4 which can be any number from 0-99.

/img/4.png?t=1351606887

What's the regex to match the number 4 and any other number only?

Upvotes: 0

Views: 1016

Answers (2)

tink
tink

Reputation: 15238

/\/[0-9]+/

Will that suffice, you think?

Upvotes: 0

Martin Ender
Martin Ender

Reputation: 44289

This really depends on how flexible your URL can be. Depending on your regex flavor you can either use lookarounds:

(?<=/img/)\d+(?=\.png)

Or a capturing group:

/img/(\d+)\.png

In the latter case you will match /img/4.png but the first captured group will contain only the 4.

Which variant you can use, and how to retrieve the capture content really depends on your language/tool.

Upvotes: 1

Related Questions