[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Get value from string

Luca Roma

2/24/2008 6:12:00 PM

I have a string that content a link of a youtube page .
Like:
http://it.youtube.com/watch?v=PupR5V9aE2s&...

i want get the value of v:
PupR5V9aE2s

How i can do?
Thanks
--
Posted via http://www.ruby-....

4 Answers

Stefano Crocco

2/24/2008 6:18:00 PM

0

Alle Sunday 24 February 2008, Luca Roma ha scritto:
> I have a string that content a link of a youtube page .
> Like:
> http://it.youtube.com/watch?v=PupR5V9aE2s&...
>
> i want get the value of v:
> PupR5V9aE2s
>
> How i can do?
> Thanks

If you know that the part of the string you want is delimited by v= and &, you
can use this:

str.match(/v=([^&]*)/)[1]

Stefano




Luca Roma

2/24/2008 6:22:00 PM

0

Thanks stefano for the responce

There are 2 possible links:
http://it.youtube.com/watch?v=P...&...
http://it.youtube.com/watch?v=P...


str.match(/v=([^&]*)/)[1]
Is your command valid also for the second link?
Thanks

Stefano Crocco wrote:
> Alle Sunday 24 February 2008, Luca Roma ha scritto:
>> I have a string that content a link of a youtube page .
>> Like:
>> http://it.youtube.com/watch?v=P...&...
>>
>> i want get the value of v:
>> PupR5V9aE2s
>>
>> How i can do?
>> Thanks
>
> If you know that the part of the string you want is delimited by v= and
> &, you
> can use this:
>
> str.match(/v=([^&]*)/)[1]
>
> Stefano

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

Stefano Crocco

2/24/2008 6:42:00 PM

0

Alle Sunday 24 February 2008, Luca Roma ha scritto:
> Thanks stefano for the responce
>
> There are 2 possible links:
> http://it.youtube.com/watch?v=P...&...
> http://it.youtube.com/watch?v=P...
>
>
> str.match(/v=([^&]*)/)[1]
> Is your command valid also for the second link?
> Thanks
>
> Stefano Crocco wrote:
> > Alle Sunday 24 February 2008, Luca Roma ha scritto:
> >> I have a string that content a link of a youtube page .
> >> Like:
> >> http://it.youtube.com/watch?v=P...&...
> >>
> >> i want get the value of v:
> >> PupR5V9aE2s
> >>
> >> How i can do?
> >> Thanks
> >
> > If you know that the part of the string you want is delimited by v= and
> > &, you
> > can use this:
> >
> > str.match(/v=([^&]*)/)[1]
> >
> > Stefano

Yes. The regexp I used looks for the string v= followed by any number of
characters which are not '&'. Those characters are put into the first group of
the returned MatchData. Since your second link doesn't contain the '&', the
match will go on until the end, which should give you what you want.

Stefano


Thomas Preymesser

2/24/2008 7:30:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On 24/02/2008, Luca Roma <roma@nerto.it> wrote:
>
> I have a string that content a link of a youtube page .
> Like:
> http://it.youtube.com/watch?v=PupR5V9aE2s&...



irb(main):001:0> require 'uri'
=> true
irb(main):002:0> res = URI.split('
http://it.youtube.com/watch?v=PupR5V9aE2s&...')
=> ["http", nil, "it.youtube.com", nil, nil, "/watch", nil,
"v=PupR5V9aE2s&test=1", nil]

gives you an array of:

* Scheme
* Userinfo
* Host
* Port
* Registry
* Path
* Opaque
* Query
* Fragment

-Thomas