1

Shouldn't this accept two different patterns, such as /hello/ and /hello/123/? The first seems to be a catch-all.

(r'^(?P<org>\S+)/$', 'path.to.view'),
(r'^(?P<org>\S+)/(?P<id>\d{3})/$', 'path.to.view'),

What I really want to find is a slug and a number of varying length (though above it shows only three characters): /hello-slug-name/123/ or just /hello-slug-name/

Edit:

A note for posterity's sake: of the two answers below, both are very helpful in for understanding what's going on here. I'm making the answer that I ultimately used in my implementation as "correct" with the green tick, but both are very insightful and helpful.

2 Answers 2

3

The first regex matches any string that consists of non-whitespace characters and ends in a slash. Therefore it matches both your strings.

The second regex matches a string that consists of non-whitespace characters, followed by a slash, followed by three digits, followed by another slash.

From your example I gather that by "number of varying length" you also mean "possibly zero (in which case the slash is also dropped)". One regex that would cover all these cases would be

^(?P<org>\S+?)/(?:(?P<id>\d+)/)?$
Sign up to request clarification or add additional context in comments.

1 Comment

I only broke it up so that I could write two separate views to handle each type of request :)
2

try using this:

(r'^(?P<org>[-A-Za-z0-9_]+)/$', 'path.to.view'),
(r'^(?P<org>[-A-Za-z0-9_]+)/(?P<id>\d+)/$', 'path.to.view'),

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.