0

I'm not very good at Regex, but I've been working on improving. Right now I'm trying to make a regex expression to match all URLs in a string that follows a certain syntax. Here is my regex code:

@http://api\.ning\.com:80/files/[a-z0-9\*]/[a-z0-9\.]\.[jpg|png|gif|bmp]@i

Here is an example of something I want this to match (but it isn't matching):

http://api.ning.com:80/files/etWx3bZZxVPTI8A3sSd3zoLhhkTmjoCs2IRFnOacPoHzJogudMCze2mB2Fib0Z*R/ScreenShot20131111at4.58.13PM.png?width=375
0

1 Answer 1

2

You need to do two things:

  1. Add a + at the end of group of characters [a-z0-9*] and [a-z0-9.]
  2. Move the file extensions in parentheses instead of square brackets

The expression would become:

@http://api\.ning\.com:80/files/[a-z0-9\*]+/[a-z0-9\.]+\.(jpg|png|gif|bmp)@i

which can be further simplified to:

@http://api\.ning\.com:80/files/[\w\*]+/[\w\.]+\.(jpg|png|gif|bmp)@

Note that \w already includes upper-case and lower-case letters as well as numbers. So, you can get rid of the i flag too unless you need it for the file extensions or the base URL.

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

7 Comments

Don't you have to escape the slaches ?!
Not in PHP. That is a Java feature. Try it out here: regex101.com/r/vT6tE6. The forward slashes don't need to be escaped since the regex delimiter here is @.
I suspected it... thks for the info :)
Nice catch at the end. I didn't look past the first error, force of habit.
Wouldn't the i flag check for different case situations for the file extensions too though?
|

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.