我有一个网址
http://example.com/embed/comments/?base=default&version=17f88c4&f=fir&t_i=article_25&t_u=http%3A%2F%2Fwww.firstpost.com%2Fsports%2Fkotla-test-22.html%09&t_e=Kotla%20Test20struggle&t_d=Kotla%20Test20struggle&t_t=Kotla%20Test20struggle&s_o=default
查询是 base,version,f,t_i,t_u,t_e,t_d,t_t,s_o
约束:
我需要找到正确的正则表达式。了解他们并想出了这个
r'^embed/comments/?base=(\w+)&version=(\w+)&f=\w+&t_i=\w+&t_u=.+&t_e=.+&t_d=.+&t_t=.+&s_o=\w+'
我正在使用django,所以在urls.py中,上面应该匹配而且确实如此。
Q.0。如何提取基础,版本等相关字段?有了约束,应该将正则表达式修改为什么?
例如,为了保存论坛,使用下面的正则表达式。我搜索了两个多小时但找不到?P<forum>
功能
Q.1。 ?P<forum>
是什么意思?
r'^forum/(?P<forum>.+)/$'
P.S。我是正则表达式的新手,请耐心等待,并用简单的术语解释。非常感谢你
答案 0 :(得分:3)
Q.0 :它们是查询参数,您不必将它们放在您的网址正则表达式中。您应该测试视图中是否缺少某些查询参数。
这是一个完整的例子:
在urls.py
文件中,请使用此正则表达式:
url(r'embed/comments/', views.your_view),
然后在您的views.py
文件中:
def your_view(request):
# get your query params like this
base = request.GET.get('base')
version = request.GET.get('version')
f = request.GET.get('f')
# then test if some parameter are missing
if base and version and f:
# do what you want
Q.1 :它是一个命名组。在django中,此语法将使您能够在视图中获取此参数。
E.g :
如果用户在您的视图中到达forum/hello-n00b
def example(request, forum):
# forum is equals to 'hello-n00b'
答案 1 :(得分:0)
使用命名组,我会这样做:
anyChar = "[^&]" # pattern to match any character inside url
necessary = ['base', 'version', 'f'] # list of necessary segments
others = ['t_i', 't_u', 't_e', 't_d', 't_d', 's_o'] # list of other allowed segments
pattern = "^embed/comments/\?" # start of pattern
# wrap all necessary to naming groups
necessaryPatterns = ["(?P<" + name + ">" + name + "=" + anyChar + "+)" for name in necessary]
# wrap all others into naming groups
othersPatterns = ["(?P<" + name + ">" + name + "=" + anyChar + "+)" for name in othersPatterns]
pattern += "&".join(necessaryPatterns) # append to pattern all necessary separated by "&" sign
pattern += "(?:&" # start optional segments with nom-matching group
pattern += ")?(?:&".join(othersPatterns) # append all others with closing-opening non-matching group marked as optional
pattern += ")?$" # end with $ to match end of string
regex = re.compile(pattern) # compile pattern
url = "your_super_long_url" # your url to match
match = re.match(pattern, url) # run match operation
if matchObj: # check if it matched
base = match.group('base') # access matched named groups easily
version = match.group('version')
....
示例可能包含错误,但它会给您基本的想法。段的名称应写为常量,包装名称可以通过函数完成,但我目前的Python技能不允许我在合理的时间内写完全级。