Parsing strings


#1

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

-Thanks


#2

“monkey pants”.gsub(/ey/,’’)
=> ‘monk pants’


#3

… I know the substring, its location, and length


#4

Daniel S. wrote:

“monkey pants”.gsub(/ey/,’’)
=> ‘monk pants’

haha, thanks, i’ll give that a try


#5

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


#6

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


#7

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


#8

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


#9

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.


#10

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


#11

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”


#12

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


#13

You could try :

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

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


#14

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


#15

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