Determinig if 3 characters in a row are the same?

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.

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>

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.

[email protected] wrote:

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

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

Should that match?

Joel VanderWerf wrote:

[email protected] 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.

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

William C. wrote:

Joel VanderWerf wrote:

[email protected] 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.

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