From: Sebastian Hungerecker Date: 2009-06-21T07:44:00+09:00 Subject: Re: replacing repetition of characters in string Am Sonntag 21 Juni 2009 00:05:22 schrieb David Rio: > INPUT > --------- > test. AA. I greatXXrr love it. > audi tt uu yes. > stop here, axmmen. > > OUTPUT > ------------ > {3} test. *AA*. I great*XX**rr* love it. > {2} audi tt uu yes. > {1} stop here, ax*mm*en. I'm assuming line 2 should be {2} audi *tt* *uu* yes DATA.each do |line| i = 0 # For each match of "any character followed by the same character" do: marked = line.gsub(/(.)\1/) do |match| i += 1 # return the match surrounded by asterisks from the block, which will # tell gsub to replace the match with that "*#{ match }*" end puts "{#{ i }} #{marked}" end __END__ test. AA. I greatXXrr love it. audi tt uu yes. stop here, axmmen. Output: {3} test. *AA*. I great*XX**rr* love it. {2} audi *tt* *uu* yes. {1} stop here, ax*mm*en. HTH, Sebastian