是否找到/lt; region>/< city>/< category>的正则表达式? [英] Finding the regex for /<region>/<city>/<category>?

查看:100
本文介绍了是否找到/lt; region>/< city>/< category>的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是发给我的

< * @ google.com>写道:

你好,尼克拉斯, 如果您只想对此进行映射:/region/city/category/ 这只是 此有效字符:[a-zA-Z0-9_] 您可以执行以下操作: -main.py

应用= webapp.WSGIApplication([('/([-\ w] +)/([-\ w] +)/([-\ w] +)', 处理程序)],debug = True)

以及您的处理程序上: 类Handler(webapp.RequestHandler): def get(自身,区域,城市,类别): #在方法上使用这些变量 希望这会有所帮助!

我的webapp可以处理<region>/<city>/<category>这样的URI,并带有可选的城市和类别,例如

/rio_de_janeiro/grande_rio_de_janeiro/casas? #<region>/<city>/<category>
/gujarat/ahmedabad/vehicles-for_sale #<region>/<city>/<category>
/rio_de_janeiro/grande_rio_de_janeiro/ #<region>/<city>
/delhi #<region>

等现在,我想启用一个请求处理程序,该处理程序可以接受由分隔符/分隔的可选参数.如果我在请求处理程序中使用正则表达式'/(.*)和变量,则第一个变量变为a/b,第二个变量变为b,这几乎是我想要的,但是a和b分别是2个不同的变量.我为请求处理程序尝试过的正则表达式是

application = webapp.WSGIApplication([('/(.*)',MyPage),

我的请求处理程序的函数头是

class MyPage(RequestHandler):
    def get(self, location='frankfurt', category='electronics'):

要启用HTTP查询,例如/frankfurt,/frankfurt/,/frankfurt/electronics,/madrid/apartments,/newyork等,允许所有可能的组合.您能给我建议一个可以实现我想要的正则表达式吗?我想要的功能类似于mod_rewrite,但适用于GAE.

谢谢

澄清

这只是使目录成为变量"的问题,因此在这里澄清一些示例,说明其行为

'/frankfurt',-将'frankfurt'放入变量1 '/frankfurt/',-将'frankfurt'放入变量1 '/frankfurt/electronics',-在变量1中放置'frankfurt',在变量2中放置'electronics' '/法兰克福/电子/',同上 '/eu/frankfurt/electronics',与上述相同,即仅最后2个组计数 '/eu/frankfurt/electronics/',与上面相同 'toronto/lightnings',不是以/开头,所以不起作用 上面的"toronto/lightnings/" 'lima/cars/old',如上所述 上面的"lima/cars/old/"

我要处理的典型案例是/region/city/category,即如果我将示例应用于巴西,可能是/rio_de_janeiro/grande_rio_de_janeiro/casas? /region/city/category或印度,可能是/delhi/delhi/for_sale或/gujarat/ahmedab​​ad/vehicles-for_sale

解决方案

据我所知,答案可以满足我的目的

/(?:[^/]+)/?([^/]*)/?([^/]*)

解决方案

您可以尝试

/(?:[^/]+)/?([^/]*)/?([^/]*)

会将'a/b'放入变量1,将'a'放入变量2,将'b'放入变量3.

This was posted to me

<*@google.com> wrote:

Hi Niklas, If you just want to map this: /region/city/category/ supposing its only this valid characters: [a-zA-Z0-9_] you can do the following: - main.py

application = webapp.WSGIApplication([('/([-\w]+)/([-\w]+)/([-\w]+)', handler)],debug=True)

and on your handler: class Handler(webapp.RequestHandler): def get(self, region, city, category): # Use those variables on the method Hope this helps!

My webapp is suppossed to handle URI like <region>/<city>/<category> with optional city and category e.g.

/rio_de_janeiro/grande_rio_de_janeiro/casas? #<region>/<city>/<category>
/gujarat/ahmedabad/vehicles-for_sale #<region>/<city>/<category>
/rio_de_janeiro/grande_rio_de_janeiro/ #<region>/<city>
/delhi #<region>

Etc Now I want to enable a request handler that can take optional arguments divided by the separator /. If I use the regex '/(.*) and variables in the request handler the first varible becomes a/b and the second variable becomes b so that is nearly what I want but a and b as 2 different variables instead. The regex I tried for the request handler is

application = webapp.WSGIApplication([('/(.*)',MyPage),

And the function head of my request handler is

class MyPage(RequestHandler):
    def get(self, location='frankfurt', category='electronics'):

To enable an HTTP query e.g. /frankfurt, /frankfurt/, /frankfurt/electronics, /madrid/apartments, /newyork, etc allowing all possible combinations. Can you advice me a regex that can achieve what I want? I want functionality like a mod_rewrite but for GAE.

Thanks

Clarification

It's just a question of "make the directory a variable" so to clarify here are some examples how it should behave

'/frankfurt', - put 'frankfurt' in variable 1 '/frankfurt/', - put 'frankfurt' in variable 1 '/frankfurt/electronics', - put 'frankfurt' in variable 1 and 'electronics' in virable 2 '/frankfurt/electronics/', same as above '/eu/frankfurt/electronics', same as above i.e. only last 2 groups count '/eu/frankfurt/electronics/', same as above 'toronto/lightnings', doesn't start with / so shan't work 'toronto/lightnings/', as above 'lima/cars/old', as above 'lima/cars/old/' as above

Typical cases I want to handle is /region/city/category i.e. if I apply the example to Brazil it could be /rio_de_janeiro/grande_rio_de_janeiro/casas? for /region/city/category or for India it could be /delhi/delhi/for_sale or /gujarat/ahmedabad/vehicles-for_sale

Solution

As far as I can tell the solution from the answer works for my purposes:

/(?:[^/]+)/?([^/]*)/?([^/]*)

解决方案

You can try

/(?:[^/]+)/?([^/]*)/?([^/]*)

which will put 'a/b' in variable 1, 'a' in variable 2 and 'b' in variable 3. Not sure if that is what you want.

这篇关于是否找到/lt; region&gt;/&lt; city&gt;/&lt; category&gt;的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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