Python 如何从此url中提取基本、版本和其他相关字段?

Python 如何从此url中提取基本、版本和其他相关字段?,python,regex,django,django-urls,Python,Regex,Django,Django Urls,我有一个网址 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=Kotl

我有一个网址

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
问题是 基础、版本、f、t_i、t_、t_e、t_d、t_t、s_o

限制条件:

  • 基础、版本、f是必需的
  • 其他t_i、t_u、t_e、t_d、t_d、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中,上面的内容应该匹配,而且确实匹配

    问题0。如何提取base、version和其他的相关字段?有了这些约束,正则表达式应该修改成什么

    例如,要保存论坛,请使用下面的正则表达式。我搜索了两个多小时,但没有找到什么
    ?P
    功能

    问题1。P是什么意思

    r'^forum/(?P<forum>.+)/$'
    
    r'^forum/(?P.+)/$”
    

    另外,我是regex的新手,请容忍我并用更简单的术语解释。非常感谢您

    Q.0:它们是查询参数,您不必将它们放在url正则表达式中。您应该测试视图中是否缺少某些查询参数

    下面是一个完整的示例:

    urls.py
    文件中,使用以下正则表达式:

    url(r'embed/comments/,views.your_视图),
    
    然后在
    views.py
    文件中:

    def您的_视图(请求):
    #像这样获取查询参数
    base=request.GET.GET('base')
    version=request.GET.GET('version')
    f=请求.GET.GET('f')
    #然后测试是否缺少某些参数
    如果基础、版本和f:
    #做你想做的
    
    Q.1:这是一个命名组。在django中,此语法将使您能够在视图中获取此参数

    例如: 如果用户访问
    论坛/hello-n00b
    ,则在您的视图中

    def示例(请求、论坛):
    #论坛等同于“hello-n00b”
    
    使用命名组,我会这样做:

    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')
        ....
    
    anyChar=“[^&]”匹配url内任何字符的模式
    必需=['base'、'version'、'f']#必需段列表
    其他=['t_i'、't_u'、't_e'、't_d'、't_d'、's_o']#其他允许分段的列表
    pattern=“^embed/comments/\?”模式的开始
    #包装命名组所需的所有内容
    必要模式=[“(?P“+name+”=“+anyChar+”+)”表示必要模式中的名称]
    #将所有其他对象包装到命名组中
    othersPatterns=[”(?P“+name+”=“+anyChar+”+)”表示othersPatterns中的名称]
    模式+=“&”.join(必要模式)#将所有必要的模式附加到模式中,并用“&”符号分隔
    模式+=”(?:&“#使用nom匹配组启动可选段
    模式+=”)(?:&“.join(othersPatterns)#附加所有其他,并将关闭-打开非匹配组标记为可选
    模式+=”?$”#以$结尾以匹配字符串结尾
    regex=re.compile(pattern)#compile pattern
    url=“你的超级长url”#你要匹配的url
    match=re.match(模式、url)#运行匹配操作
    if matchObj:#检查是否匹配
    base=match.group('base')#轻松访问匹配的命名组
    version=match.group('version')
    ....
    

    该示例可能包含错误,但它将为您提供基本概念。段的名称应写为常量,将名称包装到命名组可以通过函数完成,但我目前的Python技能不允许我在合理的时间内编写完整的类。

    ?括号内的p
    是命名组,如果该组匹配,您可以将其称为
    g
    m.group('forum')“
    see.给了你两张赞成票。”。非常感谢你。你真棒!