URI regex help

I am trying to extract the fields of a URL path.

Every URL extract example I have found online (or have tried to compose
personally) has been wrong.

here is a sample url https://api.github.com/repos/orgName/final

I want to extract “orgName” from it

On Sun, Dec 2, 2012 at 7:50 AM, Pierre-Andre M. [email protected]
wrote:

here is a sample url https://api.github.com/repos/orgName/final

I want to extract “orgName” from it

1.9.3 (main):0 >
URI.parse(“https://api.github.com/repos/orgName/final”).path.split(‘/’)[2]
=> “orgName”
1.9.3 (main):0 >

HTH!

That syntax doesnt appear to work:

1.9.3-p194 :016 >
URI.parse(“https://api.github.com/repos/orgName/final”).path.
1.9.3-p194 :017 >
URI.parse(“https://api.github.com/repos/orgName/final”).path…
1.9.3-p194 :018 >
URI.parse(“https://api.github.com/repos/orgName/final”).path…
1.9.3-p194 :019 >
URI.parse(“https://api.github.com/repos/orgName/final”).path…

url = ‘https://api.github.com/repos/orgName/final
pieces = url.split(‘/’)
p pieces
puts
puts pieces[-2]

–output:–
[“https:”, “”, “api.github.com”, “repos”, “orgName”, “final”]

orgName

On Sun, Dec 2, 2012 at 5:18 PM, Pierre-Andre M. [email protected]
wrote:

That syntax doesnt appear to work:

1.9.3-p194 :016 >
URI.parse(“https://api.github.com/repos/orgName/final”).path.
1.9.3-p194 :017 >
URI.parse(“https://api.github.com/repos/orgName/final”).path…
1.9.3-p194 :018 >
URI.parse(“https://api.github.com/repos/orgName/final”).path…
1.9.3-p194 :019 >
URI.parse(“https://api.github.com/repos/orgName/final”).path…

Yes, of course. That’s not even valid Ruby code.

$ ruby -ce
‘URI.parse(“https://api.github.com/repos/orgName/final”).path.’
-e:1: syntax error, unexpected $end
$ ruby -ce
‘URI.parse(“https://api.github.com/repos/orgName/final”).path…’
-e:1: syntax error, unexpected $end

But this is not what Hassan suggested.

Cheers

robert

2.0.0dev :001 > require ‘uri’ ;
2.0.0dev :002 >
URI.parse(“https://api.github.com/repos/orgName/final”).path.split(‘/’).grep(
/^orgName$/).join
=> “orgName”