Regex Django url正则表达式未匹配
我需要匹配url中的url编码空格,即%20 我的url应该是Regex Django url正则表达式未匹配,regex,django,django-urls,Regex,Django,Django Urls,我需要匹配url中的url编码空格,即%20 我的url应该是 http://domain/something/hello%20world 这是我的url配置和视图 url(r'^regtest/(\w+[%20]?\w+)', views.regView) 视图: 这是我在url上点击的日志 http://127.0.0.1:8000/regtest/hello%20world hello and None [13/Jan/2014 02:12:31] "GET /regtest/hel
http://domain/something/hello%20world
这是我的url配置和视图
url(r'^regtest/(\w+[%20]?\w+)', views.regView)
视图:
这是我在url上点击的日志
http://127.0.0.1:8000/regtest/hello%20world
hello and None
[13/Jan/2014 02:12:31] "GET /regtest/hello%20world HTTP/1.1" 200 3
模式
[%20]
匹配%
、2
或0
改为使用以下正则表达式来匹配单词字符(\w
)或(|
),%20
:
r'^regtest/((?:\w|%20)+)'
更新
%20
由Django解释并解码为空格(
)。因此,您应该匹配空格,而不是%20
r'^regtest/([\w\s]+)'
当我在url配置中使用它时,它甚至没有得到匹配。我对它进行了交叉检查,我也尝试了以下常规表达式。(\w+(%20)?\w+)
>>> import re
>>> matched = re.search(r'regtest/((?:\w|%20)+)', 'regtest/hello%20world')
>>> matched.group(1)
'hello%20world'
r'^regtest/([\w\s]+)'