Forum: Ruby Parsing strings

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
NB88 (Guest)
on 2007-06-18 04:43
Quick question, is there a method for deleting substrings from within a
string. If not, does anyone have any suggestions?

-Thanks
Daniel S. (Guest)
on 2007-06-18 04:47
(Received via mailing list)
"monkey pants".gsub(/ey/,'')
=> 'monk pants'
Dan G. (Guest)
on 2007-06-18 04:47
... I know the substring, its location, and length
Dan G. (Guest)
on 2007-06-18 04:48
Daniel S. wrote:
> "monkey pants".gsub(/ey/,'')
> => 'monk pants'

haha, thanks, i'll give that a try
Daniel -. (Guest)
on 2007-06-18 04:53
(Received via mailing list)
On 6/18/07, NB88 <removed_email_address@domain.invalid> wrote:
>
> Quick question, is there a method for deleting substrings from within a
> string. If not, does anyone have any suggestions?
>
> -Thanks



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
Dan G. (Guest)
on 2007-06-18 04:57
One problem, the substring contains a ?, and is left in the resultant
string
unknown (Guest)
on 2007-06-18 05:00
(Received via mailing list)
Hi --

On Mon, 18 Jun 2007, Dan G. 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
Dan G. (Guest)
on 2007-06-18 05:10
unknown wrote:
> Hi --
>
> On Mon, 18 Jun 2007, Dan G. 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
Tim H. (Guest)
on 2007-06-18 05:17
(Received via mailing list)
Dan G. 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.
Dan G. (Guest)
on 2007-06-18 07:58
Tim H. wrote:
> Dan G. 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
Banzai (Guest)
on 2007-06-18 08:10
(Received via mailing list)
On 2007-06-17 22:58:18 -0500, Dan G. <removed_email_address@domain.invalid> 
said:

> 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

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";
url.gsub(/\?.*$/,'')
http://www.youtube.com/watch
=> nil
irb(main):002:0> str="a?b?c"; str.gsub(/\?.*$/,'')
=> "a"
Robert K. (Guest)
on 2007-06-18 10:51
(Received via mailing list)
On 18.06.2007 03:10, Dan G. wrote:
>>    puts string       # => This string
>>
>>
>> David
>
> thanks, that works

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
come (Guest)
on 2007-06-18 11:46
(Received via mailing list)
You could try :

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

or with a postive lookahead : url[/watch\?(?=v=OY8AIhbqPHo)/]=""
Dan G. (Guest)
on 2007-06-18 12:03
come wrote:
> You could try :
>
> url='http://www.youtube.com/watch?v=OY8AIhbqPHo'
> 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'
Rob B. (Guest)
on 2007-06-18 15:57
(Received via mailing list)
On Jun 18, 2007, at 4:03 AM, Dan G. wrote:
> 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'

so just do that:

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

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

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

-Rob

Rob B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
This topic is locked and can not be replied to.