0

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?

1
  • Do you want to match the number 4 only if it appears before .png? Or is it okay if find the number 4 after t=? Commented Oct 30, 2012 at 18:58

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Damn it @m.buettner! Can't get a word in when you're online :D
@acheong87 sorry about that :D
is this javascript? javascript doesn't support lookbehinds (the (?<=...) thing). Use the second version instead.
0
/\/[0-9]+/

Will that suffice, you think?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.