Tornado 中 URL 模式末尾的美元符号 ($) [英] Dollar sign ($) at the end of URL patterns in Tornado

查看:41
本文介绍了Tornado 中 URL 模式末尾的美元符号 ($)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在交给我的团队的 Tornado 应用程序中看到一些 URL 正则表达式以 $ 结尾......也许我不是最懂网络的人,这对其他人来说很明显,但我没有看到在匹配整个字符串时显式指示行尾的区别(不是在内部搜索).

I've seen some URLs regexps ending with a $ in a Tornado app that was handed to my team... Maybe I'm not the most web literate guy and this is obvious for others, but I don't see the difference it makes to explicitly indicate the end of line when matching the whole string (not searching inside).

有什么我遗漏的或者只是多余的吗?

Is there something that I'm missing or it is just redundant?

编辑以更清楚我的意思:

edit to make clearer what I mean:

   handlers = [
        tornado.web.URLSpec(r'/About/$', ShowSettingsHandler),
        ...
   ]

应该完全相同:

   handlers = [
        tornado.web.URLSpec(r'/About/', ShowSettingsHandler),
        ...
   ]

因为处理程序调度程序寻找精确匹配,而不是子字符串.

as the handler dispatcher looks for an exact match, not a substring.

推荐答案

Tornado 中不需要尾随 $.我出于习惯把它们放进去,因为我认为它更清晰,但是 r"^/hello" 只会匹配/hello.

There is no need for the trailing $ in Tornado. I put them in out of habit, and because I think it's clearer, but r"^/hello" will ONLY match /hello.

我们可以通过查看 URLSpec 来源:

if not pattern.endswith('$'):
    pattern += '$'

这篇关于Tornado 中 URL 模式末尾的美元符号 ($)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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