【问题标题】:Help with a AppEngine Handler Regex?帮助 AppEngine 处理程序正则表达式?
【发布时间】:2009-10-31 22:06:00
【问题描述】:

我一直在尝试设计一个 Google AppEngine Python 处理程序正则表达式,但未能成功。

我正在尝试处理类似于OpenStreetMap's 的 API 调用。

我当前的正则表达式如下所示:

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

但是当它出现时:

/api/0.6/changeset/723/close

它错误地将723/closechangeset 分组,而我希望它把它分为三类:changeset723close

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

【问题讨论】:

    标签: python regex google-app-engine


    【解决方案1】:

    试试这个:

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

    我的 Python 测试:

    >>> 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多