2016-12-28 41 views
0

我有一个包含一堆随机URL的文件有一个GET参数:使用如何验证一个URL中使用正则表达式

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

我如何可以验证该URL包含一个GET参数,而不是一个POST正则表达式?

回答

2

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

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 

这是looooooong打印的正则表达式,你最好只使用URI类。

+0

'使用正则表达式' – user7351912

+0

'match [8]'部分做什么? – user7351912

+0

它获得第8个捕获组,恰好是查询部分。 – akuhn