【问题标题】:How to extract the related fields for base, version and others from this url?如何从此 url 中提取 base、version 和其他的相关字段?
【发布时间】:2016-03-11 05:15:12
【问题描述】:

我有一个网址

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_u、t_e、t_d、t_t、s_o

约束:

  1. base、version、f 是必需的。
  2. 其他 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 中,上面的内容应该匹配并且确实如此。

Q.0。如何提取base、version等相关字段?有了约束,正则表达式应该修改成什么?

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

Q.1。 ?P<forum> 是什么意思?

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

附:我是正则表达式的新手,请耐心等待并用更简单的术语解释。非常感谢

【问题讨论】:

  • ?P&lt;forum&gt; 括号内是命名组,如果该组匹配,您可以将其称为g&lt;forum&gt;m.group('forum)' 参见python re documentation

标签: python regex django django-urls


【解决方案1】:

使用命名组,我会这样:

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 技能不允许我在合理的时间内编写完整的类。

【讨论】:

    【解决方案2】:

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

    这是一个完整的例子:

    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 中,此语法将使您能够在视图中获取此参数。

    例如: 如果用户到达forum/hello-n00b,那么在您看来

    def example(request, forum):
        # forum is equals to 'hello-n00b'
    

    【讨论】:

    • 为你投了两票。太感谢了。你太棒了!
    猜你喜欢
    • 1970-01-01
    • 2020-02-12
    • 2021-08-14
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2017-04-01
    • 2016-09-08
    • 1970-01-01
    相关资源
    最近更新 更多