帮助AppEngine处理程序正则表达式? [英] Help with a AppEngine Handler Regex?

查看:136
本文介绍了帮助AppEngine处理程序正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图设计一个Google AppEngine Python处理程序正则表达式,并没有太成功地实现它。



我试图处理API调用类似于 OpenStreetMap's



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

但是当它出现时:

  /api/0.6/changeset/723/close  

它不正确地将> 723 / close 和 changeset 分组时,我想将它分组为三件事: changeset 723 关闭



最后一个斜杠和组是可选的,因此 /?

解决方案

试试这个:

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

p>

 >>> regex = re.compile(r^ / api / 0.6 /([^ /] +)/([^ /] +)/?([^ /] *)$)
>>> ; regex.match(/ api / 0.6 / changeset)是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)是None
True


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 /?.

解决方案

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

这篇关于帮助AppEngine处理程序正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆