Marius
Marius

Reputation: 4016

capture with if-then-else in php regex

I'm very lost with a regular expression. It's just black magic to me. Here's what i need:

my question is as follows: how do i make the "(_[a-z]?[0-9]{3,4})" part optional? I've tried adding a question mark to the second group like this:

/^([a-zA-Z_\-0-9]+)(_[a-z]?[0-9]{3,4})?\.(jpg|jpeg|png)$/

Even though the pattern works, it always captures the contents of the second group in the first group and leaves the second empty.

How can i make this work to capture the filename, advanced part (_p250) and the extension separately? I'm thinking it has something to do with the greediness of the first group, but i might be completely wrong and even if i'm right, i still don't know how to solve it.

Thanks for your thoughts

Upvotes: 0

Views: 217

Answers (2)

lmortenson
lmortenson

Reputation: 1605

Adding a question mark after the first plus will make the first capturing expression non-greedy. This worked for me using your test case:

/^([a-zA-Z_\-0-9]+?)(_[a-z]?[0-9]{3,4})?\.(jpg|jpeg|png)$/

I tested in Javascript, not PHP, but here's my test:

"some_file_p250.jpg".match(/^([a-zA-Z_\-0-9]+?)(_[a-z]?[0-9]{3,4})?\.(jpg|jpeg|png)$/)

and my results:

["some_file_p250.jpg", "some_file", "_p250", "jpg"]

In my experience, making a capturing expression non-greedy makes regular expressions a lot more intuitive and will often make them work the way I expect them to work. In your case, it was doing what you suspected; the first expression was capturing everything and never gave the second expression a chance to capture anything.

Upvotes: 0

Christian Ezeani
Christian Ezeani

Reputation: 350

I think this is what you want:

/^([a-zA-Z_\-0-9]+)(|_[a-z]?[0-9]{3,4})?\.(jpg|jpeg|png)$/

or

/^([\d\w\-]+)(|_[a-z]?[0-9]{3,4})\.(jpg|jpeg|png)$/

Upvotes: 0

Related Questions