Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Get value from string

Reply
Thread Tools

Get value from string

 
 
Luca Roma
Guest
Posts: n/a
 
      02-24-2008
I have a string that content a link of a youtube page .
Like:
http://it.youtube.com/watch?v=PupR5V9aE2s&test=1

i want get the value of v:
PupR5V9aE2s

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

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      02-24-2008
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&test=1
>
> 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




 
Reply With Quote
 
 
 
 
Luca Roma
Guest
Posts: n/a
 
      02-24-2008
Thanks stefano for the responce

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


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=PupR5V9aE2s&test=1
>>
>> 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-forum.com/.

 
Reply With Quote
 
Stefano Crocco
Guest
Posts: n/a
 
      02-24-2008
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=PupR5V9aE2s&test=1
> http://it.youtube.com/watch?v=PupR5V9aE2s
>
>
> 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=PupR5V9aE2s&test=1
> >>
> >> 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


 
Reply With Quote
 
Thomas Preymesser
Guest
Posts: n/a
 
      02-24-2008
[Note: parts of this message were removed to make it a legal post.]

On 24/02/2008, Luca Roma <(E-Mail Removed)> wrote:
>
> I have a string that content a link of a youtube page .
> Like:
> http://it.youtube.com/watch?v=PupR5V9aE2s&test=1




irb(main):001:0> require 'uri'
=> true
irb(main):002:0> res = URI.split('
http://it.youtube.com/watch?v=PupR5V9aE2s&test=1')
=> ["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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Howto identify a string value vs. a numeric value in std::string frohlinger@gmail.com C++ 9 09-19-2007 09:34 AM
Parse value from name/value pair string David Lozzi ASP .Net 0 06-08-2006 03:34 AM
hex value in string back to real hex value jack Python 4 09-08-2004 07:11 AM
String.value and String.value.length doesn't works together in IE with JVM Wee Javascript 1 07-16-2003 12:32 PM



Advertisments