need some Ruby regex help;
I'm trying to work with some cookies returning on an array, where which index return a string similar to this:
"goals_url=http://myurl.com; path=/"
Each cookies returns in an index of my array; I need some help to break it in 3 parts:
header = goals_url
value = http://myurl.com
path = /
Someone can help me with an Idea how to do this? I was trying some regex, but couldnt resolve it.
Thanks.
CodePudding user response:
url = "goals_url=http://myurl.com; path=/"
parts = url.split(";")
path = parts[1]
parts = parts[0].split("=")
header = parts[0]
value = parts[1]
Hope that's help you
CodePudding user response:
Thank you so much, Rafael. I followed your tips using split and resolved my problem, at least the first part of it:
cookies.each do |result|
path = '/'
parts = result.split(";")
main = parts[0].split("=")
name = main[0]
value = main[1]
driver.manage.add_cookie(name: "#{name}", value: "#{value}", domain: "#{domain}", path: "#{path}")
end
Thank you!
CodePudding user response:
You can use String#match
with named captures like so:
cookie ="goals_url=http://myurl.com; path=/"
cookie.match(/(?<header>. )?=(?<value>. )?;\s path=(?<path>. )?/).named_captures
#=>{"header"=>"goals_url", "value"=>"http://myurl.com", "path"=>"/"}