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.

···

--
Posted via http://www.ruby-forum.com/.

Ben Johnson 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>

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

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

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

···

On Fri, 25 Aug 2006, Ben Johnson 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.

--
to foster inner awareness, introspection, and reasoning is more efficient than
meditation and prayer.
- h.h. the 14th dalai lama

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

···

On Aug 24, 2006, at 8:02 PM, Ben Johnson 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):005:0> re = %r/(...*?)\1/
=> /(...*?)\1/
irb(main):006:0> re =~ "abab"
=> 0

Should that match?

···

ara.t.howard@noaa.gov wrote:

On Fri, 25 Aug 2006, Ben Johnson 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/

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Joel VanderWerf wrote:

···

ara.t.howard@noaa.gov 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.

--
Posted via http://www.ruby-forum.com/\.

William Crawford wrote:

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

···

ara.t.howard@noaa.gov wrote:

--
Posted via http://www.ruby-forum.com/\.