# Determinig if 3 characters in a row are the same?

**URL:** <https://www.ruby-forum.com/t/determinig-if-3-characters-in-a-row-are-the-same/66381>\
**Category:** Ruby\
**Created:** [August 25, 2006, 2:02am UTC](https://www.ruby-forum.com/t/determinig-if-3-characters-in-a-row-are-the-same/66381 "2006-08-25T02:02:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [August 25, 2006, 2:02am UTC](https://www.ruby-forum.com/t/determinig-if-3-characters-in-a-row-are-the-same/66381/1 "2006-08-25T02:02:39Z")

</div>

Sorry for the noob question, but is there a fast and efficient way to  
determine if there are 3 of the same charater in a row in a string? I’m  
sure this could be done in regex but I am not a black belt in regex.

thanks for your help.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [August 25, 2006, 2:13am UTC](https://www.ruby-forum.com/t/determinig-if-3-characters-in-a-row-are-the-same/66381/2 "2006-08-25T02:13:34Z")

</div>

Ben J. wrote:

> Sorry for the noob question, but is there a fast and efficient way to  
> determine if there are 3 of the same charater in a row in a string? I’m  
> sure this could be done in regex but I am not a black belt in regex.
> 
> thanks for your help.

irb(main):001:0\> s1 = “1234aaa567”  
=\> “1234aaa567”  
irb(main):002:0\> s2 = “1234abc567”  
=\> “1234abc567”  
irb(main):003:0\> s1 =~ /(.)\1\1/  
=\> 4  
irb(main):004:0\> s2 =~ /(.)\1\1/  
=\> nil  
irb(main):005:0\>

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [August 25, 2006, 2:19am UTC](https://www.ruby-forum.com/t/determinig-if-3-characters-in-a-row-are-the-same/66381/3 "2006-08-25T02:19:35Z")

</div>

Ben J. wrote:

> Sorry for the noob question, but is there a fast and efficient way to  
> determine if there are 3 of the same charater in a row in a string? I’m  
> sure this could be done in regex but I am not a black belt in regex.
> 
> thanks for your help.

If by “character” you mean any character then this will do the trick:

irb(main):004:0\> m = /(.)\1\1/.match(‘zyzyzxxx010101’)  
=\> #MatchData:0x1743d78  
irb(main):005:0\> m[0]  
=\> “xxx”  
irb(main):006:0\> m = /(.)\1\1/.match(‘zyzyzxyx010101’)  
=\> nil

If you meant “alphabetic character” or “alpha-numeric character” then it  
needs a bit of tweaking.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [August 25, 2006, 2:47am UTC](https://www.ruby-forum.com/t/determinig-if-3-characters-in-a-row-are-the-same/66381/4 "2006-08-25T02:47:38Z")

</div>

removed\_email\_address@domain.invalid wrote:

> re = %r/(…\*?)\1/

irb(main):005:0\> re = %r/(…_?)\1/  
=\> /(…_?)\1/  
irb(main):006:0\> re =~ “abab”  
=\> 0

Should that match?

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [August 25, 2006, 3:07am UTC](https://www.ruby-forum.com/t/determinig-if-3-characters-in-a-row-are-the-same/66381/5 "2006-08-25T03:07:00Z")

</div>

Joel VanderWerf wrote:

> removed\_email\_address@domain.invalid wrote:
> 
> > re = %r/(…\*?)\1/
> 
> irb(main):005:0\> re = %r/(…_?)\1/  
> =\> /(…_?)\1/  
> irb(main):006:0\> re =~ “abab”  
> =\> 0
> 
> Should that match?

I think he interpreted it as ‘3 characters in a row, twice in the  
string’ … So ‘abc1234abc678’ would match the ‘abc’ but ‘abab’ only  
has 2 characters repeated, so doesn’t match.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [August 25, 2006, 2:33am UTC](https://www.ruby-forum.com/t/determinig-if-3-characters-in-a-row-are-the-same/66381/6 "2006-08-25T02:33:37Z")

</div>

On Fri, 25 Aug 2006, Ben J. wrote:

> Sorry for the noob question, but is there a fast and efficient way to  
> determine if there are 3 of the same charater in a row in a string? I’m  
> sure this could be done in regex but I am not a black belt in regex.
> 
> thanks for your help.

harp:~ \> cat a.rb  
require ‘yaml’

re = %r/(…\*?)\1/

%w[  
a  
ab  
abc  
abcd  
abcabc  
abcdabcd  
abcdeabcde  
].each{|word| y “word” =\> word, “word[re]” =\> word[re]}

harp:~ \> ruby a.rb  
word: a  
word[re]:  
word: ab  
word[re]:  
word: abc  
word[re]:  
word: abcd  
word[re]:  
word: abcabc  
word[re]: abcabc  
word: abcdabcd  
word[re]: abcdabcd  
word: abcdeabcde  
word[re]: abcdeabcde

kind regards.

-a

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [August 25, 2006, 3:08am UTC](https://www.ruby-forum.com/t/determinig-if-3-characters-in-a-row-are-the-same/66381/7 "2006-08-25T03:08:22Z")

</div>

William C. wrote:

> Joel VanderWerf wrote:
> 
> > removed\_email\_address@domain.invalid wrote:
> > 
> > > re = %r/(…\*?)\1/
> > 
> > irb(main):005:0\> re = %r/(…_?)\1/  
> > =\> /(…_?)\1/  
> > irb(main):006:0\> re =~ “abab”  
> > =\> 0
> > 
> > Should that match?
> 
> I think he interpreted it as ‘3 characters in a row, twice in the  
> string’ … So ‘abc1234abc678’ would match the ‘abc’ but ‘abab’ only  
> has 2 characters repeated, so doesn’t match.

Sorry, that’s 3 (or more) characters in a row, then the same characters  
again, with nothing between them. So ‘abc1234abc678’ would not match.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [August 25, 2006, 3:15am UTC](https://www.ruby-forum.com/t/determinig-if-3-characters-in-a-row-are-the-same/66381/8 "2006-08-25T03:15:39Z")

</div>

Here is a minor variation that’s good to keep in mind should you ever  
need find a lot more than 3 chars in row. Saves typing a whole lot of  
'\1’s.

# Find 8 of the same char in a row

/(.)\1{7}/ =~ ‘zyzyzxxxxxxxxzyzyz’  
p $& #=\> “xxxxxxxx”  
/(.)\1{7}/ =~ ‘zyzyzxxxxxxzyzyz’  
p $& #=\> nil

Regards, Morton
