我正在尝试从此网址获取“令牌”值
标记= EC-6HV41821M6960550G
url = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-6HV41821M6960550G"
我知道它可以通过正则表达式来完成,但我认为它可以通过一些URI方法来完成。
答案 0 :(得分:4)
可以这样做:
require 'uri'
url = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-xxx"
uri = URI.parse(url)
params = CGI::parse(uri.query)
params['token'] # => 'EC-xxx'
答案 1 :(得分:2)
只是为了展示,还存在一些其他方式: -
#!/usr/bin/env ruby
require 'uri'
url = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-xxx"
uri = URI(url)
URI.decode_www_form(uri.query).to_h['token']
# => "EC-xxx"
阅读方法::decode_www_form
docomentation。
答案 2 :(得分:0)
require 'cgi'
url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-6HV41821M6960550G'
CGI.parse(url)['token']