Ah, your last sentence is what led me to the bug. $uri->path from my example returns the URL without the URL parameters (i.e. without everything after the question mark). I didn't discover this until I created some tests similar to the one you posted. Good regex, bad input.
Anyway, I ended up using the following regex (from one of the answers) because it is what I was eventually aiming for:
Thank you to everyone for your help.