How do I write a RESTful url regex in GAE / Python for n parameters?

I currently have three URL paths that show up in the ServiceHandler. How can I combine three into one neat regex that can pass n number of arguments to the ServiceHandler?

(r'/s/([^/]*)', ServiceHandler),
(r'/s/([^/]*)/([^/]*)', ServiceHandler),
(r'/s/([^/]*)/([^/]*)/([^/]*)', ServiceHandler)

      

+2


source to share


3 answers


(r'^/s/(([^/]*)((/[^/]+)*))$', ServiceHandler)

      

Gotta do the trick to fit any number

/ s / foo / bar / baz / to / infinity / and / beyond /

You can also limit it to a range by doing something like

^/s/(([^/]*)((/[^/]+){0,2}))$

      



It will only match what you like

/s/foo/bar/baz
/s/foo/bar
/s/foo

      

but not

/s/foo/bar/baz/pirate
/s

      

+1


source


You can try something like

(r'/s/([^/]*)/?([^/]*)/?([^/]*)', ServiceHandler)



I think you will always get 3 ServiceHandler parameters, but those not used will be empty

0


source


This should work for any number

(r'(?<!^)/([^/]+)', ServiceHandler)

      

Since I was looking at urlresolvers.py I can see that this does not work, although you can fix the correct behavior in urlresolvers.py by using regex.findall instead of re.search.

0


source







All Articles