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

···

--
Posted via http://www.ruby-forum.com/.

1.9.3 (main):0 >
URI.parse("https://api.github.com/repos/orgName/final"\).path.split('/')[2]
=> "orgName"
1.9.3 (main):0 >

HTH!

···

On Sun, Dec 2, 2012 at 7:50 AM, Pierre-Andre M. <lists@ruby-forum.com> wrote:

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

I want to extract "orgName" from it

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

twitter: @hassan

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....

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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

$ ruby -ce 'URI.parse("https://api.github.com/repos/orgName/final&quot;\).path.'
-e:1: syntax error, unexpected $end
$ ruby -ce 'URI.parse("https://api.github.com/repos/orgName/final&quot;\).path..'
-e:1: syntax error, unexpected $end

But this is not what Hassan suggested.

Cheers

robert

···

On Sun, Dec 2, 2012 at 5:18 PM, Pierre-Andre M. <lists@ruby-forum.com> wrote:

That syntax doesnt appear to work:

1.9.3-p194 :016 >
URI.parse("https://api.github.com/repos/orgName/final&quot;\).path.
1.9.3-p194 :017 >
URI.parse("https://api.github.com/repos/orgName/final&quot;\).path..
1.9.3-p194 :018 >
URI.parse("https://api.github.com/repos/orgName/final&quot;\).path...
1.9.3-p194 :019 >
URI.parse("https://api.github.com/repos/orgName/final&quot;\).path....

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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"