Search code examples
pythonwebapp2

Non-greedy matching in webapp2 routes?


I have two routes in a Python app that I'm working on, and I can't seem to make the matching work correctly. The routes look like:

webapp2.Route('/(.*?)/(.*?)', grouploader.Loader),
webapp2.Route(r'/edit/(.*?)/(.*?)', handler='grouploader.Editor'),

I'd like for urls like /a/b to go to the first and /edit/a/b to go to the second. Apparently the matches here are greedy, since the first rule always matches, returning me "edit/a" and "b" for the two values. Is there a way to make this work, or am I barking up the wrong tree?


Solution

  • Invert the routes, and don't use lazy matching, you have another much better tool for that:

    webapp2.Route('/edit/([^/]+)/([^/]+)', handler = 'grouploader.Editor');
    webapp2.Route('/([^/]+)/([^/]+)', grouploader.Loader);