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 14 days 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 14 days ago
Ops! Sorry. Just read the cant as can... my bad.
kostasx 14 days ago
Thanks for the tip!
dekkard 14 days 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 14 days 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 14 days ago
Oops, missed that. Will update asap.
dekkard 14 days ago
Just in case, my solution was updated and seems to work fine.
dekkard 14 days ago
Winning solution

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

Something like this?

Try this: https://acme.com/cammins-1022321231231123 =)
dekkard 14 days ago
Now this: https://acme7.com/cammins-1022 XD
dekkard 14 days ago
fancy! ill award you and bonus dekkard for helping!
Qdev 14 days ago
That's very kind! Thank you! 😁
Chris Werner Rau 14 days ago
View Timeline