Richlewis Richlewis - 3 months ago 10
Ruby Question

split url based on characters - Ruby

I was wondering how best to go about splitting the following string by

/
but ignoring certain characters

So my string (well url) will look like this

url = http://10.0.3.2/i/av/Genymotionvbox86p/android/10~3~1/0/-/-/a3306aa6~0346~4ad5~bdf5~1bc7c20a88ab/0/test~page/-/-/video/live/-/one_hd/-/0/1/0~0/-/0~0/0~0/?trace=skwthffdsy


I want to split by
/
ignoring the
http://
and the query string at the end, the desired outcome would look like

["i", "av", "Genymotionvbox86p", "android", "10~3~1", "0", "-", "-",
"a3306aa6~0346~4ad5~bdf5~1bc7c20a88ab", "0", "test~page", "-", "-",
"video", "live", "-", "one_hd", "-", "0", "1", "0~0", "-", "0~0", "0~0"]


So at the moment
url.split('/')
gets me so far but its the excluding or certain characters i am stuck on

Maybe using
scan
would serve me better?

Any help appreciated

Thanks

Answer

Try this:

require 'uri'
URI(url).path.gsub(%r{^/}, "").split("/")