在 rails 中,尝试进行多次替换,但只有第一个替换
In rails, trying to make multiple substitutions but only the first one is taking
我正在尝试对一个变量进行多次替换,其值为
https://api.mydomein.com/events/EVENTID/profiles?max=10&total=1&appid=1234f5bf9f36bc4a1dc8fce2&token=9997aa817ff66b96f3956835f17941e1&search=4001&func=na&parms=%7B%22browser%22%3Afalse%7D&settings=%7B%22setWait%22%3Afalse%7D&_=1468271558064
我尝试以这种方式进行替换……
object_desc_link = OBJECT_DESC_LINK_TEMPLATE.sub( %r{events\/([^\])+}, "events/#{@event_id}" )
.sub( %r{appid=([^\&])+}, "appid=#{@app_id}" )
.sub( %r{token=([^\&])+}, "token=#{@token}" )
.sub( %r{search=([^\&])+}, "search=#{i}" )
但是在这个语句之后是 运行 值是
https://api.mydomein.com/events/EVENTID
出于某种原因,第一个替换是切断 URL 的最后一部分。如何保留整个字符串并在我指定的位置进行替换?
你搞砸了你的正则表达式。应该是
%r{events/([^/])+}
你有 \
在那里。 url.
的路径部分没有反斜杠
顺便说一句,你为什么不使用适当的工具来完成这项工作?解析 urls 是一个已解决的问题。
s = 'https://api.mydomein.com/events/EVENTID/profiles?max=10&total=1&appid=1234f5bf9f36bc4a1dc8fce2&token=9997aa817ff66b96f3956835f17941e1&search=4001&func=na&parms=%7B%22browser%22%3Afalse%7D&settings=%7B%22setWait%22%3Afalse%7D&_=1468271558064'
require "addressable/uri"
uri = Addressable::URI.parse(s)
h = uri.query_values # => {"max"=>"10", "total"=>"1", "appid"=>"1234f5bf9f36bc4a1dc8fce2", "token"=>"9997aa817ff66b96f3956835f17941e1", "search"=>"4001", "func"=>"na", "parms"=>"{\"browser\":false}", "settings"=>"{\"setWait\":false}", "_"=>"1468271558064"}
h['appid'] = 'whatever'
uri.query_values = h
uri.path = uri.path.sub('EVENTID', '42')
uri.to_s # => "https://api.mydomein.com/events/42/profiles?_=1468271558064&appid=whatever&func=na&max=10&parms=%7B%22browser%22%3Afalse%7D&search=4001&settings=%7B%22setWait%22%3Afalse%7D&token=9997aa817ff66b96f3956835f17941e1&total=1"
我正在尝试对一个变量进行多次替换,其值为
https://api.mydomein.com/events/EVENTID/profiles?max=10&total=1&appid=1234f5bf9f36bc4a1dc8fce2&token=9997aa817ff66b96f3956835f17941e1&search=4001&func=na&parms=%7B%22browser%22%3Afalse%7D&settings=%7B%22setWait%22%3Afalse%7D&_=1468271558064
我尝试以这种方式进行替换……
object_desc_link = OBJECT_DESC_LINK_TEMPLATE.sub( %r{events\/([^\])+}, "events/#{@event_id}" )
.sub( %r{appid=([^\&])+}, "appid=#{@app_id}" )
.sub( %r{token=([^\&])+}, "token=#{@token}" )
.sub( %r{search=([^\&])+}, "search=#{i}" )
但是在这个语句之后是 运行 值是
https://api.mydomein.com/events/EVENTID
出于某种原因,第一个替换是切断 URL 的最后一部分。如何保留整个字符串并在我指定的位置进行替换?
你搞砸了你的正则表达式。应该是
%r{events/([^/])+}
你有 \
在那里。 url.
顺便说一句,你为什么不使用适当的工具来完成这项工作?解析 urls 是一个已解决的问题。
s = 'https://api.mydomein.com/events/EVENTID/profiles?max=10&total=1&appid=1234f5bf9f36bc4a1dc8fce2&token=9997aa817ff66b96f3956835f17941e1&search=4001&func=na&parms=%7B%22browser%22%3Afalse%7D&settings=%7B%22setWait%22%3Afalse%7D&_=1468271558064'
require "addressable/uri"
uri = Addressable::URI.parse(s)
h = uri.query_values # => {"max"=>"10", "total"=>"1", "appid"=>"1234f5bf9f36bc4a1dc8fce2", "token"=>"9997aa817ff66b96f3956835f17941e1", "search"=>"4001", "func"=>"na", "parms"=>"{\"browser\":false}", "settings"=>"{\"setWait\":false}", "_"=>"1468271558064"}
h['appid'] = 'whatever'
uri.query_values = h
uri.path = uri.path.sub('EVENTID', '42')
uri.to_s # => "https://api.mydomein.com/events/42/profiles?_=1468271558064&appid=whatever&func=na&max=10&parms=%7B%22browser%22%3Afalse%7D&search=4001&settings=%7B%22setWait%22%3Afalse%7D&token=9997aa817ff66b96f3956835f17941e1&total=1"