Forum: Ruby URI regex help

Posted by Pierre-Andre M. (pierreandre_m)
on 2012-12-02 16:50
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 by Hassan Schroeder (Guest)
on 2012-12-02 17:05
(Received via mailing list)
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

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

HTH!
Posted by Pierre-Andre M. (pierreandre_m)
on 2012-12-02 17:17
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 by Robert Klemme (robert_k78)
on 2012-12-02 17:21
(Received via mailing list)
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").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
Posted by 7stud -- (7stud)
on 2012-12-02 21:52
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 by Stu (Guest)
on 2012-12-02 22:38
(Received via mailing list)
2.0.0dev :001 > require 'uri' ;
2.0.0dev :002 >
URI.parse("https://api.github.com/repos/orgName/final").path....
/^orgName$/).join
=> "orgName"
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.