Get value from string

Discussion in 'Ruby' started by Luca Roma, Feb 24, 2008.

  1. Luca Roma

    Luca Roma Guest

    Luca Roma, Feb 24, 2008
    #1
    1. Advertising

  2. 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
    Stefano Crocco, Feb 24, 2008
    #2
    1. Advertising

  3. Luca Roma

    Luca Roma Guest

    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/.
    Luca Roma, Feb 24, 2008
    #3
  4. 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
    Stefano Crocco, Feb 24, 2008
    #4
  5. [Note: parts of this message were removed to make it a legal post.]

    On 24/02/2008, Luca Roma <> 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
    Thomas Preymesser, Feb 24, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    589
    James Kanze
    Sep 19, 2007
  2. Rui Maciel
    Replies:
    2
    Views:
    3,022
    AndrewDover
    Dec 1, 2009
  3. Eskimo
    Replies:
    0
    Views:
    149
    Eskimo
    Feb 7, 2005
  4. Mike Brearley

    how to get value from page.asp?value=myvalue

    Mike Brearley, May 14, 2004, in forum: ASP General
    Replies:
    1
    Views:
    130
    Mike Brearley
    May 14, 2004
  5. Wee
    Replies:
    1
    Views:
    150
    Richard Cornford
    Jul 16, 2003
Loading...

Share This Page