[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Determinig if 3 characters in a row are the same?

Ben Johnson

8/25/2006 12:03:00 AM

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

7 Answers

Joel VanderWerf

8/25/2006 12:13:00 AM

0

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

Tim Hunter

8/25/2006 12:19:00 AM

0

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.

Ara.T.Howard

8/25/2006 12:33:00 AM

0

Joel VanderWerf

8/25/2006 12:47:00 AM

0

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/

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

Should that match?

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

William Crawford

8/25/2006 1:07:00 AM

0

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

William Crawford

8/25/2006 1:08:00 AM

0

William Crawford wrote:
> 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.

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

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

Morton Goldberg

8/25/2006 1:13:00 AM

0

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.