# Replacing repetition of characters in string

**URL:** <https://www.ruby-forum.com/t/replacing-repetition-of-characters-in-string/170682>\
**Category:** Ruby\
**Created:** [June 21, 2009, 12:06am UTC](https://www.ruby-forum.com/t/replacing-repetition-of-characters-in-string/170682 "2009-06-21T00:06:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Rio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc79bd/32.png) [@David\_Rio](https://www.ruby-forum.com/u/David_Rio)\
**Post date:** [June 21, 2009, 12:06am UTC](https://www.ruby-forum.com/t/replacing-repetition-of-characters-in-string/170682/1 "2009-06-21T00:06:26Z")

</div>

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:

> <https://gist.github.com/133299>

Do you guys have any other alternatives?

-drd

---

<div class="post-metadata">

**Author:** ![David\_Rio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc79bd/32.png) [@David\_Rio](https://www.ruby-forum.com/u/David_Rio)\
**Post date:** [June 21, 2009, 12:42am UTC](https://www.ruby-forum.com/t/replacing-repetition-of-characters-in-string/170682/2 "2009-06-21T00:42:41Z")

</div>

Hi –

On Sun, 21 Jun 2009, David Rio wrote:

> audi tt uu yes.  
> [133299’s gists · GitHub](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

---

<div class="post-metadata">

**Author:** ![David\_Rio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc79bd/32.png) [@David\_Rio](https://www.ruby-forum.com/u/David_Rio)\
**Post date:** [June 21, 2009, 12:45am UTC](https://www.ruby-forum.com/t/replacing-repetition-of-characters-in-string/170682/3 "2009-06-21T00:45:01Z")

</div>

Am Sonntag 21 Juni 2009 00:05:22 schrieb David Rio:

> {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

---

<div class="post-metadata">

**Author:** ![David\_Rio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc79bd/32.png) [@David\_Rio](https://www.ruby-forum.com/u/David_Rio)\
**Post date:** [June 21, 2009, 5:19am UTC](https://www.ruby-forum.com/t/replacing-repetition-of-characters-in-string/170682/4 "2009-06-21T05:19:21Z")

</div>

This problem is a little under-specified, and it would be worth asking  
exactly what your friend is trying to accomplish.  
As a quick question, what should happen if the input contains THREE of  
the  
same character? Should “aaa” become  
aaa  
_aa_a  
_aaa_  
_a_a_a_  
or something else?

---

<div class="post-metadata">

**Author:** ![David\_Rio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc79bd/32.png) [@David\_Rio](https://www.ruby-forum.com/u/David_Rio)\
**Post date:** [June 21, 2009, 1:57am UTC](https://www.ruby-forum.com/t/replacing-repetition-of-characters-in-string/170682/5 "2009-06-21T01:57:31Z")

</div>

On Sat, Jun 20, 2009 at 5:44 PM, Sebastian  
Hungerecker[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > {2} audi tt uu yes.  
> > {1} stop here, ax_mm_en.
> 
> I’m assuming line 2 should be {2} audi _tt_ _uu_ yes

Yes, that’s right. I don’t know why it got removed from my initial post.

> 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.

Ah… much better. Thanks sebastian.

-drd

---

<div class="post-metadata">

**Author:** ![David\_Rio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc79bd/32.png) [@David\_Rio](https://www.ruby-forum.com/u/David_Rio)\
**Post date:** [June 21, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/replacing-repetition-of-characters-in-string/170682/6 "2009-06-21T13:38:28Z")

</div>

On Sat, Jun 20, 2009 at 10:18 PM, Morris Keesan[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> This problem is a little under-specified,

Yes, my bad.

> and it would be worth asking  
> exactly what your friend is trying to accomplish.  
> As a quick question, what should happen if the input contains THREE of the  
> same character? Should “aaa” become  
> aaa  
> _aa_a  
> _aaa_  
> _a_a_a_  
> or something else?

_aa_a, which is what Sebastian’s code does.

-drd

---

<div class="post-metadata">

**Author:** ![David\_Rio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc79bd/32.png) [@David\_Rio](https://www.ruby-forum.com/u/David_Rio)\
**Post date:** [June 21, 2009, 1:51pm UTC](https://www.ruby-forum.com/t/replacing-repetition-of-characters-in-string/170682/7 "2009-06-21T13:51:20Z")

</div>

Hi –

On Sun, 21 Jun 2009, David Rio wrote:

> > _aa_a  
> > _aaa_  
> > _a_a_a_  
> > or something else?
> 
> _aa_a, which is what Sebastian’s code does.

Mine does too (I lucked out 🙂

str = “Helllo”  
str.gsub(/((\w)\2)/,’_\1_’) # “He_ll_lo”

David
