为了确保我的应用程序运行顺利,我尝试匹配多个 URL,如下所示:
我有以下获取功能:
class Competition(Handler):
def get(self, *a):
if a:
print a
else:
print 'None'
app = webapp2.WSGIApplication([('/', Index),
('/competitions[/]?([^/]+)[/]?([^/]+)[/]?([^/]*)$', Competition),
],debug=True)
我目前使用的正则表达式是这个的修改版本:Jed Smith help others
并得到以下输出:
/competitions/one/two/three = ('one', 'two', 'three') - 预期结果
/competitions/one/two = ('one', 'two', '') - 预期结果
/competitions/one/ = ('on', 'e', '') - 意外结果
/competitions/one = ('on', 'e', '') - 意外结果
/competitions/ = 404 - 不匹配模式
/competitions = 404 - 不匹配模式
理想情况下,我希望 Regex 从匹配基本 url 开始:
localhost:8080/competitions(带有或不带有尾随“/”)到比赛处理程序
然后添加 0 个或多个我可以测试的参数... 0 将匹配我的 if 语句的开头,并且将根据需要处理以下语句。
我可以将它限制为 *a 中的 3 个最大变量(即:/competitions/one/two/three 是最大值)但是为了更好地学习如何处理这些东西,我想学习如何使它匹配尽可能多输入。
任何帮助,将不胜感激。正则表达式让我感到困惑。
干杯
- 编辑以修复格式