Parsing strings

Quick question, is there a method for deleting substrings from within a
string. If not, does anyone have any suggestions?

-Thanks

···

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

"monkey pants".gsub(/ey/,'')
=> 'monk pants'

···

-----Original Message-----
From: list-bounce@example.com
[mailto:list-bounce@example.com] On Behalf Of NB88
Sent: Monday, 18 June 2007 10:43 AM
To: ruby-talk ML
Subject: Parsing strings

Quick question, is there a method for deleting substrings
from within a
string. If not, does anyone have any suggestions?

-Thanks

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

... I know the substring, its location, and length

···

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

the gsub or gsub! methods will do this. They take a pattern (string or
regexp) and replace it with whatever you specify. In this case an empty
string.

http://www.ruby-doc.org/core/classes/String.html#M000839

e.g.

"The quck brown fox".gub( /brown/, "" ) #=> "The quick fox"

Cheers
Daniel

···

On 6/18/07, NB88 <germans88@yahoo.com> wrote:

Quick question, is there a method for deleting substrings from within a
string. If not, does anyone have any suggestions?

-Thanks

Daniel Sheppard wrote:

"monkey pants".gsub(/ey/,'')
=> 'monk pants'

haha, thanks, i'll give that a try

···

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

One problem, the substring contains a ?, and is left in the resultant
string

···

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

Hi --

···

On Mon, 18 Jun 2007, Dan German wrote:

... I know the substring, its location, and length

You can use slice!.

   string = "This is a string"
   string.slice!(4,5)
   puts string # => This string

David

--
* Books:
   RAILS ROUTING (new! http://safari.awprofessional.com/9780321509246\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

unknown wrote:

Hi --

... I know the substring, its location, and length

You can use slice!.

   string = "This is a string"
   string.slice!(4,5)
   puts string # => This string

David

thanks, that works

···

On Mon, 18 Jun 2007, Dan German wrote:

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

Dan German wrote:

One problem, the substring contains a ?, and is left in the resultant string

It would help if you'd show an actual string you have and what you'd like it to become. If you have two examples, even better.

···

--
RMagick OS X Installer [http://rubyforge.org/projects/rmagick/\]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618\]
RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html\]

Tim Hunter wrote:

Dan German wrote:

One problem, the substring contains a ?, and is left in the resultant
string

It would help if you'd show an actual string you have and what you'd
like it to become. If you have two examples, even better.

It's actually a youtube url: ex.

I'm trying to remove the watch? string; the slice method works, but it
would be better if you could use something like gsub

···

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

Alternative:

irb(main):001:0> string = "This is a string"
=> "This is a string"
irb(main):002:0> string[4,5]=''
=> ""
irb(main):003:0> string
=> "This string"

Kind regards

  robert

···

On 18.06.2007 03:10, Dan German wrote:

unknown wrote:

Hi --

On Mon, 18 Jun 2007, Dan German wrote:

... I know the substring, its location, and length

You can use slice!.

   string = "This is a string"
   string.slice!(4,5)
   puts string # => This string

David

thanks, that works

gsub(/\?.*$/,'') will delete the first question mark until the end of the string.

irb(main):001:0> url = "http://www.youtube.com/watch?v=OY8AlhbqPHo&quot;; url.gsub(/\?.*$/,'')
YouTube
=> nil
irb(main):002:0> str="a?b?c"; str.gsub(/\?.*$/,'')
=> "a"

···

On 2007-06-17 22:58:18 -0500, Dan German <germans88@yahoo.com> said:

Tim Hunter wrote:

Dan German wrote:

One problem, the substring contains a ?, and is left in the resultant
string

It would help if you'd show an actual string you have and what you'd
like it to become. If you have two examples, even better.

It's actually a youtube url: ex.
http://www.youtube.com/watch?v=OY8AIhbqPHo
I'm trying to remove the watch? string; the slice method works, but it
would be better if you could use something like gsub

--
H. Asari

You could try :

url='http://www.youtube.com/watch?v=OY8AIhbqPHo&#39;
url[/watch\?/]=""

or with a postive lookahead : url[/watch\?(?=v=OY8AIhbqPHo)/]=""

···

On 18 juin, 05:58, Dan German <german...@yahoo.com> wrote:

It's actually a youtube url: ex.http://www.youtube.com/watch?v=OY8AIhbqPHo
I'm trying to remove the watch? string; the slice method works, but it
would be better if you could use something like gsub

--
Posted viahttp://www.ruby-forum.com/.

come wrote:

You could try :

url='http://www.youtube.com/watch?v=OY8AIhbqPHo&#39;
url[/watch\?/]=""

or with a postive lookahead : url[/watch\?(?=v=OY8AIhbqPHo)/]=""

Thanks...that last expression worked well but I have one last question:
I didn't realize this before but I also need to remove the equal sign
after the v and replace it with a forward slash ('/') so that it looks
like 'http://www.youtube.com/v/OY8AIhbqPHo&#39;

···

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

so just do that:

url.sub(/watch\?v=/, 'v/')

or if the variable wasn't always 'v':

url.sub(/watch\?(.)=/, '\1/')

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Jun 18, 2007, at 4:03 AM, Dan German wrote:

come wrote:

You could try :

url='http://www.youtube.com/watch?v=OY8AIhbqPHo&#39;
url[/watch\?/]=""

or with a postive lookahead : url[/watch\?(?=v=OY8AIhbqPHo)/]=""

Thanks...that last expression worked well but I have one last question:
I didn't realize this before but I also need to remove the equal sign
after the v and replace it with a forward slash ('/') so that it looks
like 'http://www.youtube.com/v/OY8AIhbqPHo&#39;