Reputation: 473
I have a url: http://domain.com/home/audio/14/1/file.mp3
But the strings inside the last to sub directories could be different, such as:
http://domain.com/home/audio/142/14/file.mp3 or
http://domain.com/home/audio/67/424/file.mp3
The formatting # of sub directories will be the same, but I need to be able to get the file.mp3 regardless of what the /xx/xx/ has. I'm trying to build a Regex in PHP to accommodate this, some kind of wildcard in place of the characters that could be in those sub directories. Something like:
http://domain.com/home/audio/[wildcardregexhere]/[wildcardregexhere]/file.mp3
Sorry, I've never done Regex before. Thanks for the help.
Upvotes: 0
Views: 2169
Reputation: 538
There are a lot of possibilities, one is to match all characters instead of slash /
. How will you express this? Like [^\/]*
. In the brackets you list all chars, that should match, but with ^
at start you negate the list. So you want to match other than slash (you have to escape it, so we use \/
instead of single /
). And with the asterisk we say, that there could be zero, one or a lot of characters other than slash - its the wildcard you are looking for.
So how will the whole regexp look like?
http:\/\/domain.com\/home\/audio\/[^\/]*\/[^\/]*\/.*\.mp3
I guess, that file
could any match, so I used .*
.
In perl or php syntax it will be like:
/http:\/\/domain.com\/home\/audio\/[^\/]*\/[^\/]*\/.*\.mp3/
In Java you have to double quote (escape the escape character backslash \\
).
Good luck!
Upvotes: 2