Reputation: 3411
My goal:
To capture the last part of a URL whether there is or isn't a trailing slash, without the trailing slash being a part of the string on a URL similar to the one following:
http://foo.com/p/dPWjiVtX-C/
^^^^^^^^^^
The string I want
My issue:
Every way I try only allows for a trailing slash and not for a url without a trailing slash or makes the trailing slash be contained in the string I want.
What have I tried?
1. I have tried to add a slash to the end:
$regex = "/.*?foo\.com\/p\/(.*)\//";
if ($c=preg_match_all ($regex, $url, $matches))
{
$id=$matches[1][0];
print "ID: $id \n";
}
This results in error when I don't have a trailing slash.
2. I have tried to add a question mark:
$regex = "/.*?foo\.com\/p\/(.*)[\/]?/";
This results in the slash, if exists, being inside my string.
My question/tl;dr:
How can I build a RegEx to not require a slash, yet keep the slash out of my preceding string?
Upvotes: 4
Views: 6127
Reputation: 71578
Something you might try perhaps:
([^\/]+)\/?$
EDIT: Huh, you should have mentioned you need to check the site as well, since you put foo.com
in your first example string... (and re-edited your question after that...).
You can use this instead to check the site:
^.*foo\.com.*?([^\/]+)\/?$
Upvotes: 2
Reputation: 3106
Your .*
is greedy by default, so if it can "eat" the slash in the capturing group, it will.
To make it not greedy, you need .*?
in the place of the .*
in your capturing group. So, your regex will be:
$regex = "/^.*?instagram\.com\/p\/(.*?)[\/]?$/";
Upvotes: 9
Reputation: 149030
You can use this to capture all characters except the trailing slash in your group:
$regex = "/.*?instagram\.com\/p\/([^\/]*)/"
Or alternatively, you can use a non-greedy quantifier in your group, you'll have to specify a trailing slash or the end of the string (or some other terminator) in order for the group to capture your id:
$regex = "/.*?instagram\.com\/p\/(.*?)(?:\/|$)/"
Upvotes: 2