Matching all occurrences of a regex

This was a bit stupid of me. So I’m blogging about it to avoid being this stupid again. I was working on my own little Rack middleware and i needed to get the contents of the url after the domain name i.e get the bolded strings here: http://www.pissoff.com/yes/why/dont/ya. I had the regex working ok and it was working on Rubular though  it wasn’t showing the matches i.e it was highlighting them but not listing them down as it should do usually. Here is the regex:

/[^\/]+/

The problem is I was using the Regexp class to match it and this was only producing the first match and returning all others as nil. Maybe I was using it in the wrong way, I don’t know. What I’m certain of is the fact that the String class worked much better.

So in a nutshell, I used the String class’ scan method to get all my matches as shown below:

url = “/admin/product/show/012”
url.scan(/[^\/]+/) { |match|
puts match
}

OUTPUT

admin
product
show
012