【问题标题】:How to check if an url has numbers with fileEntityId or not?如何检查网址是否包含带有 fileEntityId 的数字?
【发布时间】:2013-01-24 04:22:14
【问题描述】:

我有一些网址如下:

https://example.com/file/filegetrevision.do?fileEntityId=738007
9&cs=4Pzbb2jPu3EHBzv8RQHrGcPm4hZZkRC-CfH0my4dP0M.arv


https://example.com/file/filegetrevision.do?fileEntityId=&cs=2L
5cx4UsMsFJgM05pPtB_Z8dRdL4CXLLcTeDhGPDBIg.arv


https://example.com/file/filegetrevision.do?fileEntityId=2555874&cs=2L
5cx4UsMsFJgM05pPtB_Z8dRdL4CXLLcTeDhGPDBIg.arv

现在我需要检查哪个网址有numbersfileEntityId,或者哪个没有?在这方面有什么帮助吗?假设第一个和第三个 URL 有 7380072555874 数字和 fileEntityId,但第二个没有。

谢谢

【问题讨论】:

    标签: ruby ruby-1.9


    【解决方案1】:

    尽管我很喜欢正则表达式,但标准库中包含了更合适的工具:

    require 'uri'
    require 'cgi'
    
    url = "https://example.com/file/filegetrevision.do?fileEntityId=7380079&cs=4Pzbb2jPu3EHBzv8RQHrGcPm4hZZkRC-CfH0my4dP0M.arv"
    query = URI::parse(url).query # => "fileEntityId=7380079&cs=4Pzbb2jPu3EHBzv8RQHrGcPm4hZZkRC-CfH0my4dP0M.arv"
    fileEntityId = CGI::parse(query)['fileEntityId'] # => ["7380079"]
    

    然后你可以检查它是否是一个数字。

    【讨论】:

    • +1 给你!我没有被介绍给这个 libaray。我已经尝试过@sergio showwn!
    • 啊。 CGI::parse!确实,这样更好:)
    【解决方案2】:

    这里我们使用正则表达式来判断一个字符串是否包含“fileEntityId=”后跟一个或多个数字:

    urls = ['https://example.com/file/filegetrevision.do?fileEntityId=7380079&cs=4Pzbb2jPu3EHBzv8RQHrGcPm4hZZkRC-CfH0my4dP0M.arv',
      'https://example.com/file/filegetrevision.do?fileEntityId=&cs=2L5cx4UsMsFJgM05pPtB_Z8dRdL4CXLLcTeDhGPDBIg.arv',
      'https://example.com/file/filegetrevision.do?fileEntityId=2555874&cs=2L5cx4UsMsFJgM05pPtB_Z8dRdL4CXLLcTeDhGPDBIg.arv']
    
    
    urls.map {|u| !!(u =~ /fileEntityId=\d+/)} # => [true, false, true]
    

    【讨论】:

    • +1 给你纠正我的,我忘了把+ 在我的一个。因此我的没有工作!
    猜你喜欢
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 2012-04-12
    • 2013-06-21
    • 1970-01-01
    • 2012-08-30
    相关资源
    最近更新 更多