From: "David A. Black" Date: 2009-06-21T07:42:10+09:00 Subject: Re: replacing repetition of characters in string Hi -- On Sun, 21 Jun 2009, David Rio wrote: > Hi there, > > A friend of mine asked me to write a script that reads a file and > marks the double characters. > Something like: > > $ ruby scratch_ruby_drio.rb > 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 coded something up that works but I don't like it: > > http://gist.github.com/133299 > > Do you guys have any other alternatives? Try this: str.gsub(/((\w)\2)/,'*\1*') (Change \w to whatever represents the characters you want to capture.) David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Now available: The Well-Grounded Rubyist (http://manning.com/black2) "Ruby 1.9: What You Need To Know" Envycasts with David A. Black http://www.envycasts.com