开发者

Help with a AppEngine Handler Regex?

开发者 https://www.devze.com 2022-12-10 09:23 出处:网络
I\'ve been trying to design a Google AppEngine Python handler regex and haven\'t开发者_Python百科 been too successful in getting it to work.

I've been trying to design a Google AppEngine Python handler regex and haven't开发者_Python百科 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 /?.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消