Determinig if 3 characters in a row are the same?

B

Ben Johnson

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

Joel VanderWerf

Ben said:
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>
 
T

Timothy Hunter

Ben said:
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.
 
A

ara.t.howard

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
 
J

Joel VanderWerf

harp:~ > cat a.rb
require 'yaml'

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

irb(main):005:0> re = %r/(...*?)\1/
=> /(...*?)\1/
irb(main):006:0> re =~ "abab"
=> 0

Should that match?
 
W

William Crawford

Joel said:
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.
 
W

William Crawford

William said:
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.
 
M

Morton Goldberg

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,210
Messages
2,571,091
Members
47,692
Latest member
RolandRose

Latest Threads

Top