newbie question: deleting all of a string's contents BEFORE a certain character

S

superwick

Hi,

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 = "
"
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
 
D

dblack

Hi --

Hi,

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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
J

Joel VanderWerf

superwick said:
Hi,

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 = "
"
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.
 
S

superwick

I was trying to figure out how to take a string like this:
"
"
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 = "
"
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("
")
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
 
P

Phrogz

Hi,

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

irb(main):003:0> s = "
"
=> "
"
irb(main):004:0> s =~ /[^=]+$/
=> 31
irb(main):005:0> s[ /[^=]+$/ ]
=> "QMuClxuWdH8"
 
J

Joshua Ballanco

Chad said:
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

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
 
D

dblack

Hi --

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+$/

:)


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
J

Joshua Ballanco

unknown said:
Hi --



Actually it's even easier:

YOUTUBE_PARSER = /\w+$/

:)


David

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

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top