[英]how to match a particular pattern using regex?
I am using django and in urls.py
there is a regex pattern: 我正在使用django并且在
urls.py
有一个正则表达式模式:
url(r'^note/(\d+)/$', 'publicnote'),
url(r'^note/$', 'publicnote'),
this works fine. 这很好。 But i decided to add a name to url.
但我决定在url中添加一个名称。 so i redesigned the regex:
所以我重新设计了正则表达式:
url(r'^note/(\d*)/?$', 'publicnote',name='public_note'),
but the problem is this will match note//
. 但是问题是这将匹配
note//
。
so is there any regex so that it will match only note/
and note/<an integer>/
因此是否有任何正则表达式,使其仅匹配
note/
和note/<an integer>/
This regexp should work: 这个正则表达式应该工作:
r'^note/(?:(\d+)/)?$'
The ?
?
makes the previous group optional. 使上一组可选。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.