Newbie question: deleting all of a string's contents BEFORE

Hi,

I was trying to figure out how to take a string like this: “http://
- YouTube
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 = “- YouTube
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?

Thanks in advance!
-C

Hi –

On Thu, 7 Jun 2007, superwick wrote:

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?

Try this:

code = url.sub(/.*=/, “”)

which means: replace all characters, up to and including =, with
nothing (""). Another way is to grab a substring, matching all non-=
characters up to the end of the string:

code = url[/[^=]+\z/]

[^=] is a character class consisting of all characters other than =.

  • means match one or more, and \z anchors the whole pattern to the end
    of the string.

David

superwick wrote:

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?

url[/\w+$/]

There are many ways to do this. This one makes sense if you know for
sure that you want to grab all “word” characters (that is, [A-Za-z0-9_])
at the end of the string.

On Jun 6, 2:44 pm, superwick [email protected] wrote:

Hi,

I was trying to figure out how to take a string like this: “- YouTube
and removing everything before (and including) the “=”, creating a new
string with just “QMuClxuWdH8”.

irb(main):003:0> s = “- YouTube
=> “- YouTube
irb(main):004:0> s =~ /[^=]+$/
=> 31
irb(main):005:0> s[ /[^=]+$/ ]
=> “QMuClxuWdH8”

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(“- YouTube”)
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

Chad Wells wrote:

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! :smiley:

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

-C

Don’t forget, regular expressions in Ruby are objects (…well, yes,
everything in Ruby is objects…). So, you could do something like:

YOUTUBE_PARSER = Regexp.(/\w+$/)

url[YOUTUBE_PARSER]

That way, if Youtube ever does change their naming scheme, you only have
to change one line of code (and that line can be put right up on the top
of your listing).

Hope that helps a bit!

-Josh

Hi –

On Thu, 7 Jun 2007, Joshua B. wrote:

-C

Don’t forget, regular expressions in Ruby are objects (…well, yes,
everything in Ruby is objects…). So, you could do something like:

YOUTUBE_PARSER = Regexp.(/\w+$/)

Actually it’s even easier:

YOUTUBE_PARSER = /\w+$/

:slight_smile:

David

unknown wrote:

Hi –

On Thu, 7 Jun 2007, Joshua B. wrote:

-C

Don’t forget, regular expressions in Ruby are objects (…well, yes,
everything in Ruby is objects…). So, you could do something like:

YOUTUBE_PARSER = Regexp.(/\w+$/)

Actually it’s even easier:

YOUTUBE_PARSER = /\w+$/

:slight_smile:

David

Hmm…true…in fact, I left out the “new” (should have been
Regexp.new(/\w+$/) )…so…yeah…

On Jun 6, 5:16 pm, “Ball, Donald A Jr (Library)”
[email protected] wrote:

code = rev_chomped.reverse
require ‘cgi’
url = URI::parse(“- YouTube”)
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! :smiley:

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

-C