update regex to find ID's just in the last segment of the url
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

We have this regex today https://regex101.com/r/vEHFi9/2 It works pretty well but you can see it fails some urls in the middle of the samples

/2019/google-says-its-achieved-quantum-supremacy-but-ibm-says-nope-2313521/

/2019/google-says-its-achieved-quantum-supremacy-but-ibm-says-nope/

/6848464/2019/google-says-its-achieved-quantum-supremacy-but-ibm-says-nope/

It should be looking only in the last segment for an ID. 2 of the 3 examples here should have no id and the first one should have an id of 2313521

Ideally your update will properly make the story_id only select from the last segment of the url string. As you will also notice some of the last segments dont have a trailing / and just immediately in to params so we have to be sure this update doesnt get tripped up by that.

awarded to Wuddrum
Tags
regex

Crowdsource coding tasks.

1 Solution

Winning solution

Hey Qdev, how's this? https://regex101.com/r/01hhrr/1

Added (?:[^\d\/]) before the id check, so it excludes any ID's that start with a / or more numbers (in cases when the number is longer than specified {4,9}).
Wuddrum 1 month ago
as always - thanks!
Qdev 1 month ago