python---正则中的(?Pgroup)

时间:2023-03-10 03:23:59
python---正则中的(?P<name>group)
application=tornado.web.Application([
(r"/index/(?P<num>\d*)/(?P<nid>\d*)",home.IndexHandler),
],**settings)

结合函数

class IndexHandler(tornado.web.RequestHandler):
def get(self,nid='',num=''):
print(nid,num)
self.write('<h1>hello world</h1>')
(?P<num>\d*)是将后面匹配的\d*数据,取一个组名,这个组名必须是唯一的,不重复的,没有特殊符号
函数可以获得这个组名下的数据,一种就是按照上面路由中的形式传递和被解析
另一种是正则匹配使用group(组名)可以获取到这个组名下的数据,同group(1),group(2)类似