I want to match a url that has extra characters after a particular base url string. The base can be either '/home' or '/home/accounts/{numeric_account_id}'. I don't need to capture any matches; I just want to test whether there is a match.
I made a regex of:
r'^/home(/accounts/\d+)?(/[A-Za-z0-9_\-]+)+/?$'
Some examples are:
With my regex above, it incorrectly matches 3 & 4. I tried changing the '?' after accounts group to '*', but that doesn't help. It seems that the last group is greedily matching cases 3 & 4.
What is the correct regex? Thanks
Try this regex:
^/home(?:(?!/accounts/)|/accounts/\d+)(?:/[A-Za-z0-9_\-]+)+/?$
It will match /home
, then either:
(?!/accounts/)
: an empty string that doesn't have /accounts/
coming next, or
/accounts/\d+
: /accounts/
followed by numbers.
After that, match any characters allowed in the path.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句