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

**URL:** <https://www.ruby-forum.com/t/newbie-question-deleting-all-of-a-strings-contents-before/106158>\
**Category:** Ruby\
**Created:** [June 6, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/newbie-question-deleting-all-of-a-strings-contents-before/106158 "2007-06-06T22:45:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [June 6, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/newbie-question-deleting-all-of-a-strings-contents-before/106158/1 "2007-06-06T22:45:48Z")

</div>

Hi,

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

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [June 6, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/newbie-question-deleting-all-of-a-strings-contents-before/106158/2 "2007-06-06T22:55:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [June 6, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/newbie-question-deleting-all-of-a-strings-contents-before/106158/3 "2007-06-06T23:05:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [June 6, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/newbie-question-deleting-all-of-a-strings-contents-before/106158/4 "2007-06-06T23:31:53Z")

</div>

On Jun 6, 2:44 pm, superwick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I was trying to figure out how to take a string like this: “[- YouTube](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 = “[- YouTube](http://www.youtube.com/watch?v=QMuClxuWdH8)”  
=\> “[- YouTube](http://www.youtube.com/watch?v=QMuClxuWdH8)”  
irb(main):004:0\> s =~ /[^=]+$/  
=\> 31  
irb(main):005:0\> s[/[^=]+$/ ]  
=\> “QMuClxuWdH8”

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [June 6, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/newbie-question-deleting-all-of-a-strings-contents-before/106158/5 "2007-06-06T23:16:51Z")

</div>

> 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](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

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [June 7, 2007, 1:50am UTC](https://www.ruby-forum.com/t/newbie-question-deleting-all-of-a-strings-contents-before/106158/6 "2007-06-07T01:50:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [June 7, 2007, 2:24am UTC](https://www.ruby-forum.com/t/newbie-question-deleting-all-of-a-strings-contents-before/106158/7 "2007-06-07T02:24:17Z")

</div>

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

🙂

David

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [June 7, 2007, 2:49am UTC](https://www.ruby-forum.com/t/newbie-question-deleting-all-of-a-strings-contents-before/106158/8 "2007-06-07T02:49:52Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [June 6, 2007, 11:32pm UTC](https://www.ruby-forum.com/t/newbie-question-deleting-all-of-a-strings-contents-before/106158/9 "2007-06-06T23:32:53Z")

</div>

On Jun 6, 5:16 pm, “Ball, Donald A Jr (Library)”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > code = rev\_chomped.reverse  
> > require ‘cgi’  
> > url = URI::parse(“[- YouTube](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

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

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

-C
