Reputation: 15384
I was wondering how best to go about splitting the following string by /
but ignoring certain characters
So my string 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?
Upvotes: 1
Views: 453
Reputation: 3830
Try this:
require 'uri'
URI(url).path.sub(%r{^/}, "").split("/")
Edit: why use gsub
when shorter and faster sub
is perfectly fine.
Upvotes: 5
Reputation: 4615
maybe something like this?
url.split('/')[3..-2]
[3..-2]
means scope of array, it takes all elements from index 3
to index -2
-2
means one before last
Upvotes: 1