[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

superwick

6/6/2007 8:45:00 PM

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=QMuClxu...
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

3 Answers

dblack

6/6/2007 8:54:00 PM

0

Joel VanderWerf

6/6/2007 9:05:00 PM

0

superwick 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".
>
> At first I thought I would have to do something like:
>
> url = "http://www.youtube.com/watch?v=QMuClxu...
> 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.

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

Gavin Kistner

6/6/2007 9:28:00 PM

0

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=QMuClxu...
> 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=QMuClxu...
=> "http://www.youtube.com/watch?v=QMuClxu...
irb(main):004:0> s =~ /[^=]+$/
=> 31
irb(main):005:0> s[ /[^=]+$/ ]
=> "QMuClxuWdH8"