Get ID from url path - regex
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Need some help getting IDs out of urls. Seems they can be 4-7 digits and i have the following rules . if possible can you use this https://regex101.com/ so we can easily test it.

here are the rules

1- 4-7 digits

2- cant be immediately followed by - or text

3- cant be after ?

4- cant be after #

here are some test urls

https://acme.com/cammins-1022

https://acme.com/francr-cameron-cummins-1022#home

https://acme.com/franchiummins-1025642?somthing

https://acme.com/cammins-1022

https://acme.com/francr-cameron-cummins-1022#home-321321-sadfg

https://acme.com/franchiummins-1025642?somthing-321321

https://acme.com/franchiummins-1025642-?somthing-321321

In the last 3 lines, do we have multiple IDs that must be captured?
kostasx 2 months ago
in last example none would be valid so it would fail. the ID before the ? has a - after it so it fails rule #2
Qdev 2 months ago
Ops! Sorry. Just read the cant as can... my bad.
kostasx 2 months ago
Thanks for the tip!
dekkard 2 months ago
Tags
regex

Crowdsource coding tasks.

2 Solutions


Will this do?

    http[^#\?]+[^\d](\d{4,7})(?![-\d\w]+)

You'll need to capture the first group out of it.

Updated.

this is really close, its still getting an ID out of this url test https://acme.com/franchiummins-1025642-?somthing-321321 which should fail from rule #2
Qdev 2 months ago
this is really close, its still getting an ID out of this url test https://acme.com/franchiummins-1025642-?somthing-321321 which should fail from rule #2 . tested here if it helps https://regex101.com/r/XcuXFT/1
Qdev 2 months ago
Oops, missed that. Will update asap.
dekkard 2 months ago
Just in case, my solution was updated and seems to work fine.
dekkard 2 months ago
Winning solution

https://regex101.com/r/LkDdb9/2

Something like this?

Try this: https://acme.com/cammins-1022321231231123 =)
dekkard 2 months ago
Now this: https://acme7.com/cammins-1022 XD
dekkard 2 months ago
fancy! ill award you and bonus dekkard for helping!
Qdev 2 months ago
That's very kind! Thank you! 😁
Chris Werner Rau 2 months ago
Hey Chris, any chance you can make a modification to make sure the ID is only found from the last url segment? Meaning domain.com/segment1/segment2/.../last-segment/ we won’t know how many segments are in a url we are parsing but we are always sure it’s the outer most and not an inner segment.
Qdev 1 month ago
View Timeline