From: dblack@... Date: 2007-06-07T05:54:10+09:00 Subject: Re: newbie question: deleting all of a string's contents BEFORE a certain character Hi -- On Thu, 7 Jun 2007, 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=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? 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)