I'm trying to write a regex to match the numbers in these URLs (12345678
and 1234567890
).
http://www.example.com/p/12345678
http://www.example.com/p/12345678?foo=bar
http://www.example.com/p/some-text-123/1234567890?foo=bar
Rules:
/p/
in themMy attempt:
\/p\/([0-9]+)
That matches the first and second, but not the third. So I tried:
\/p\/[^\/?]*\/?([0-9]+)
No joy.
REGEX 101
Regex might not be the right tool for this job. It looks like in every case, splitting the URL with a URL parser would make more sense. From your examples, it appears that the number portion is always the last item in the path portion of the URL. I'm not sure what language you're using, but many languages offer functions that can parse URLs into their constituent parts.
$path = parse_url($url, PHP_URL_PATH);
if(strpos($path, "/p/") === 0) {
$base = basename($path);
} else {
// error
}
Works every time, assuming $url is the string you are parsing.
I extended your version, it now works with all examples:
\/p\/(.+\/)*(\d+)(\?.+=.+(&.+=.+)*)?$
If you don't care that the URL is valid, you could shrink the regex to:
\/p\/(.+\/)*(\d+)($|\?)
https://regex101.com/r/pW5qB3/2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With