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.
403ece33c07b1964f7247b735c2fa836?d=identicon&s=25 NB88 (Guest)
on 2007-06-18 02:43
Quick question, is there a method for deleting substrings from within a
string. If not, does anyone have any suggestions?

-Thanks
1c1e3bdfe006a22214102fcd6434a012?d=identicon&s=25 Daniel Sheppard (Guest)
on 2007-06-18 02:47
(Received via mailing list)
"monkey pants".gsub(/ey/,'')
=> 'monk pants'
403ece33c07b1964f7247b735c2fa836?d=identicon&s=25 Dan German (nb88)
on 2007-06-18 02:47
... I know the substring, its location, and length
403ece33c07b1964f7247b735c2fa836?d=identicon&s=25 Dan German (nb88)
on 2007-06-18 02:48
Daniel Sheppard wrote:
> "monkey pants".gsub(/ey/,'')
> => 'monk pants'

haha, thanks, i'll give that a try
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-06-18 02:53
(Received via mailing list)
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



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
403ece33c07b1964f7247b735c2fa836?d=identicon&s=25 Dan German (nb88)
on 2007-06-18 02:57
One problem, the substring contains a ?, and is left in the resultant
string
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-06-18 03:00
(Received via mailing list)
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
403ece33c07b1964f7247b735c2fa836?d=identicon&s=25 Dan German (nb88)
on 2007-06-18 03:10
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
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2007-06-18 03:17
(Received via mailing list)
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.
403ece33c07b1964f7247b735c2fa836?d=identicon&s=25 Dan German (nb88)
on 2007-06-18 05:58
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
B987e0a3278ebd51364b197ab1f0ae9e?d=identicon&s=25 Banzai (Guest)
on 2007-06-18 06:10
(Received via mailing list)
On 2007-06-17 22:58:18 -0500, Dan German <germans88@yahoo.com> 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"
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-06-18 08:51
(Received via mailing list)
On 18.06.2007 03:10, Dan German 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
0bbfc4e9c95f7248c2f281344383f2a3?d=identicon&s=25 come (Guest)
on 2007-06-18 09: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)/]=""
403ece33c07b1964f7247b735c2fa836?d=identicon&s=25 Dan German (nb88)
on 2007-06-18 10: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'
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-06-18 13:57
(Received via mailing list)
On Jun 18, 2007, at 4:03 AM, Dan German 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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.