Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Ruby Programming Language

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


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

Thanks in advance!
-C

Hi --

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)

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.

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

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

On Jun 6, 5:16 pm, "Ball, Donald A Jr (Library)"

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

On Jun 6, 2:44 pm, superwick <superw@gmail.com> wrote:

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

irb(main):003:0> s = "http://www.youtube.com/watch?v=QMuClxuWdH8"
=> "http://www.youtube.com/watch?v=QMuClxuWdH8"
irb(main):004:0> s =~ /[^=]+$/
=> 31
irb(main):005:0> s[ /[^=]+$/ ]
=> "QMuClxuWdH8"

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

--
Posted via http://www.ruby-forum.com/.

Hi --

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)

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

--
Posted via http://www.ruby-forum.com/.

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc