【问题标题】:How to verify if a URL has a GET parameter using a regex如何使用正则表达式验证 URL 是否具有 GET 参数
【发布时间】:2017-05-13 06:49:15
【问题描述】:

我有一个包含一堆随机 URL 的文件:

https://google.com
http://fakesite.net/php?id=2
https://example.com/
http://anotherexample.com/pho?admin=2

如何使用正则表达式验证 URL 是否包含 GET 参数而不是 POST?

【问题讨论】:

    标签: ruby regex file url


    【解决方案1】:

    你的意思是它是否有查询参数?

    require 'uri'
    
    if URI('http://fakesite.net/php').query
      ...
    end
    

    如果您坚持使用正则表达式,请使用此

    match = URI.regexp.match('http://fakesite.net/php?id=2')
    if match[8]
      ...
    end
    

    你可以打印正则表达式

    puts URI.regexp
    

    太麻烦了,你最好只使用URI 类。

    【讨论】:

    • Using a regular expression
    • match[8] 部分有什么作用?
    • 得到第8个捕获组,恰好是查询部分。
    猜你喜欢
    • 1970-01-01
    • 2011-03-04
    • 2016-06-05
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    相关资源
    最近更新 更多