Regex may not be the best tool for this job. It seems that in any case, splitting the URL using the URL parser will make more sense. As you can see from your examples, the numeric part is always the last element in the path of the URL path. I'm not sure which language you use, but many languages ββoffer features that can parse URLs in their component parts.
$path = parse_url($url, PHP_URL_PATH); if(strpos($path, "/p/") === 0) { $base = basename($path); } else {
It works every time, assuming $ url is the string you are casting.
source share