On Jun 6, 5:16 pm, "Ball, Donald A Jr (Library)"
<donald.b... / nashville.gov> wrote:
> > I was trying to figure out how to take a string like this:
> > "http://www.youtube.com/watch?v=QMuClxuWdH8"
> > and removing everything before (and including) the "=",
> > creating a new string with just "QMuClxuWdH8".
>
> > At first I thought I would have to do something like:
>
> > url = "http://www.youtube.com/watch?v=QMuClxuWdH8"
> > rev_url = url.reverse
> > rev_chomped = rev_url.chomp("=")
> > code = rev_chomped.reverse
>
> > ...which doesn't actually work of course and is ugly anyway.
> > I have a feeling I'll need to use regular expressions to do
> > this, but I'm really lost when it comes to regex. How could I
> > do something like this?
>
> You could use regexps if you like, or you could take advantage of ruby's
> URI and CGI libraries:
>
> require 'uri'
> require 'cgi'
> url = URI::parse("http://www.youtube.com/watch?v=foobar")
> params = CGI::parse(url.query)
> code = params['v'][0]
>
> Regexps are more straightforward, URI/CGI libs are more robust. Either
> should do for ya.
>
> - donald

That would work great if I was not worried that Youtube would change
their URL scheme (it doesn't seem too likely, given the abundance of
links out there, but should one ever fully count it out?). Not saying
it'll even always have an equals sign in it and then the unique video
indentifier at the end, either, so I guess it doesn't even matter! :-D

Anyway, thanks everyone!! I am going to give "code = url.sub(/.*=/,
"")" a shot to see how it works for me.

-C