[英]Extract multiple patterns from a URL string
我有一个URL字符串:
http://localhost:3000/user/event?profile_id=2&profile_type=UserProfile
我想提取“ 2”和“ UserProfile”,这些可以更改。
我尝试同时使用match
和scan
但均未返回结果:
url = "http://localhost:3000/user/event?profile_id=2&profile_type=UserProfile"
m = /http(s)?:\/\/(.)+\/user\/event?profile_id=(\d)&profile_type=(\w)/.match(url)
=> nil
url.scan /http(s)?:\/\/(.)+\/user\/event?profile_id=(\d)&profile_type=(\w)/
=> []
知道我做错了什么吗?
不要使用模式来尝试执行此操作。 查询参数的URL顺序可以更改,并且与位置无关,这会立即破坏模式。
而是使用为此目的而设计的工具,例如内置URI :
require 'uri'
uri = URI.parse('http://localhost:3000/user/event?profile_id=2&profile_type=UserProfile')
Hash[URI::decode_www_form(uri.query)].values_at('profile_id', 'profile_type')
# => ["2", "UserProfile"]
通过这种方式,可以确保始终以期望的顺序接收正确的值,从而轻松分配它们:
profile_id, profile_type = Hash[URI::decode_www_form(uri.query)].values_at('profile_id', 'profile_type')
以下是中间步骤,因此您可以看到发生了什么:
uri.query # => "profile_id=2&profile_type=UserProfile"
URI::decode_www_form(uri.query) # => [["profile_id", "2"], ["profile_type", "UserProfile"]]
Hash[URI::decode_www_form(uri.query)] # => {"profile_id"=>"2", "profile_type"=>"UserProfile"}
match = url.match(/https?:\/\/.+?\/user\/event\?profile_id=(\d)&profile_type=(\w+)/)
p match.captures[0] #=> '2'
p match.captures[1] #=> 'UserProfile'
在您的表情中:
/http(s)?:\/\/(.)+\/user\/event?profile_id=(\d)&profile_type=(\w)/
您放入()中的所有内容都会以正则表达式捕获。 无需将s放在括号中,因为? 将仅对前一个字符起作用。 另外,也不需要(。),因为+只会作用于前一个字符。 同样,(\\ w)应该是(\\ w +),它基本上表示:一个或多个字符(“ UserProfile”为1个或多个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.