From: huseyin polat Date: 2006-03-03T09:01:39+09:00 Subject: Re: gsub problem unknown wrote: > On Fri, 3 Mar 2006, huseyin polat wrote: > >> I have spend over 4 hours for this.. here is the question masters, very >> simple. >> str=String.new >> str="[1,2][2,1][3,1][4,1]" >> >> now I want to print this string as >> [1,2],[2,1],[3,1],[4,1] >> in other word I want to add comma between those ][ and make them look >> like ],[ >> I tried gsub with hundred different possibilities but no luck, > > harp:~ > ruby -e' puts("[1,2][2,1][3,1][4,1]".gsub(%r/\]\[/, "],[")) ' > [1,2],[2,1],[3,1],[4,1] > > >> second problem >> if my the same str has >> [1,2][2,1][3,1][4,1], >> and I want to get rid of last comma from the list, I tried chop chomp >> and all that but it gets ri of alot more than comma at the end. how can >> I get rid of last comma from the list? from [1,2][2,1][3,1][4,1], to >> [1,2][2,1][3,1][4,1] > > harp:~ > ruby -e' > puts("[1,2][2,1][3,1][4,1],,,".gsub(%r/,+$/,"").gsub(%r/\]\[/, "],[")) > ' > [1,2],[2,1],[3,1],[4,1] > > regards. > > -a thank you, however your first solution didn't make any difference and the second got rid of all the commas. thank you anyway. I have tried your second comments as str.gsub(%r/,+$/,"").gsub(%r/\]\[/, "],[") I don't know why. thank you for trying -- Posted via http://www.ruby-forum.com/.