So I’m using Django to build a webservice, but I’ve got a problem with the regexp in Urls.
I want to go to the localhost/user/USERNAME/products/ where the username can be any character, not just \w. I tried capturing it using this:
('^user/(?P<username>.+)/$', 'user'),
('^user/(?P<username>.+)/products/$', 'get_products'),
but ofc it captures the username ‘USERNAME/products’ and sends it to user. How should I say every possible character EXCEPT front slash for any length except zero?
Use
[^/]+instead of.+.As Chris Pratt notes below,
.+?will also work, as it is a “non-capturing” regex, which will match any character until a match for the following sub-expression. I generally prefer normal eager solutions to non-capturing solutions where possible, because they do not introduce multiple evaluation rules.