Get value from string

L

Luca Roma

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
 
S

Stefano Crocco

Alle Sunday 24 February 2008, Luca Roma ha scritto:
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

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
 
L

Luca Roma

Thanks stefano for the responce

There are 2 possible links:


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

Stefano said:
Alle Sunday 24 February 2008, Luca Roma ha scritto:
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

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
 
S

Stefano Crocco

Alle Sunday 24 February 2008, Luca Roma ha scritto:
Thanks stefano for the responce

There are 2 possible links:


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

Stefano said:
Alle Sunday 24 February 2008, Luca Roma ha scritto:
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

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
 
T

Thomas Preymesser

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

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



irb(main):001:0> require 'uri'
=> true
irb(main):002:0> res = URI.split('
')
=> ["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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top