# Get value from string

**URL:** <https://www.ruby-forum.com/t/get-value-from-string/130036>\
**Category:** Ruby\
**Created:** [February 24, 2008, 7:12pm UTC](https://www.ruby-forum.com/t/get-value-from-string/130036 "2008-02-24T19:12:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [February 24, 2008, 7:12pm UTC](https://www.ruby-forum.com/t/get-value-from-string/130036/1 "2008-02-24T19:12:00Z")

</div>

I have a string that content a link of a youtube page .  
Like:

i want get the value of v:  
PupR5V9aE2s

How i can do?  
Thanks

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [February 24, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/get-value-from-string/130036/2 "2008-02-24T19:18:35Z")

</div>

Alle Sunday 24 February 2008, Luca R. ha scritto:

> I have a string that content a link of a youtube page .  
> Like:  
> [The Great Trafalgar Square Freeze - YouTube](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

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [February 24, 2008, 7:22pm UTC](https://www.ruby-forum.com/t/get-value-from-string/130036/3 "2008-02-24T19:22:08Z")

</div>

Thanks stefano for the responce

There are 2 possible links:

[![](https://img.youtube.com/vi/PupR5V9aE2s/hqdefault.jpg "The Great Trafalgar Square Freeze") ](https://www.youtube.com/watch?v=PupR5V9aE2s)

[![](https://img.youtube.com/vi/PupR5V9aE2s/hqdefault.jpg "The Great Trafalgar Square Freeze") ](https://www.youtube.com/watch?v=PupR5V9aE2s)

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

Stefano C. wrote:

> Alle Sunday 24 February 2008, Luca R. ha scritto:
> 
> > I have a string that content a link of a youtube page .  
> > Like:  
> > [The Great Trafalgar Square Freeze - YouTube](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

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [February 24, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/get-value-from-string/130036/4 "2008-02-24T20:31:11Z")

</div>

On 24/02/2008, Luca R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a string that content a link of a youtube page .  
> Like:  
> [The Great Trafalgar Square Freeze - YouTube](http://it.youtube.com/watch?v=PupR5V9aE2s&test=1)

irb(main):001:0\> require ‘uri’  
=\> true  
irb(main):002:0\> res = URI.split(’  
[The Great Trafalgar Square Freeze - YouTube](http://it.youtube.com/watch?v=PupR5V9aE2s&test=1)’)  
=\> [“http”, nil, “[it.youtube.com](http://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

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [February 24, 2008, 7:42pm UTC](https://www.ruby-forum.com/t/get-value-from-string/130036/5 "2008-02-24T19:42:19Z")

</div>

Alle Sunday 24 February 2008, Luca R. ha scritto:

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