Help with a AppEngine Handler Regex?

时光毁灭记忆、已成空白 提交于 2020-01-03 03:20:08

问题


I've been trying to design a Google AppEngine Python handler regex and haven't been too successful in getting it to work.

I'm trying to handle API calls similar to OpenStreetMap's.

My current regex looks like this:

/api/0.6/(.*?)/(.*?)\/?(.*?)

But when this comes in:

/api/0.6/changeset/723/close

It incorrectly groups 723/close and changeset, when I wanted it to group it into three things: changeset, 723, and close.

The last slash and group is optional, thus the /?.


回答1:


Try this:

^/api/0.6/([^/]+)/([^/]+)/?([^/]*)$

My Python tests:

>>> regex = re.compile(r"^/api/0.6/([^/]+)/([^/]+)/?([^/]*)$")
>>> regex.match("/api/0.6/changeset") is None
True
>>> regex.match("/api/0.6/changeset/723").groups()
('changeset', '723', '')
>>> regex.match("/api/0.6/changeset/723/close").groups()
('changeset', '723', 'close')
>>> regex.match("/api/0.6/changeset/723/close/extragroup") is None
True


来源:https://stackoverflow.com/questions/1655745/help-with-a-appengine-handler-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!