6 Replies Latest reply on Dec 6, 2012 12:40 PM by philmodjunk

# 3 or more repeating Characters

### Title

3 or more repeating Characters

### Post

Anyone know how to create a function that would check to see if there are 3 or more repeating characters in a text field?

Here is an example:

Name Field: Billl

or Clifffford

This should either warn me or remove the anything with more than 3 or more repating characters.

The results should be Bill and Clifford

• ###### 1. Re: 3 or more repeating Characters

If so, this custom function will do the trick:

// RepeatThree ( TheText )
//
// Recursively scans TheText and returns True if 3 repeating characters are found
//
Case ( Length ( TheText ) < 3 ; False ; // no repeats found
Left ( TheText ; 3 ) = Left ( TheText ; 1 ) & Left ( TheText ; 1 ) & Left ( TheText ; 1 ) ; True ;  // Repeats found
RepeatThree ( Right (TheText ; Length ( TheText ) - 1 ) )
)

• ###### 2. Re: 3 or more repeating Characters

Note: a modified version could return the position of the first character of the left most pattern of three repeating characters.

• ###### 3. Re: 3 or more repeating Characters

WOW!!! Thanks sooo much, thats perfect, works very well. Can you please just explain to me how you get the function to Recursively scan?

I understand what this does and its brilliant

( Length ( TheText ) < 3 ; False ; // no repeats found
Left ( TheText ; 3 ) = Left ( TheText ; 1 ) & Left ( TheText ; 1 ) & Left ( TheText ; 1 ) ; True ;  // Repeats found

How does the function know to check more than the first 3 or the last 3 repeating characters. How is it able to pick up the 3 repeating characters that are in the middle of a 50 length field?

• ###### 4. Re: 3 or more repeating Characters

That's what happens here:

RepeatThree ( Right (TheText ; Length ( TheText ) - 1 )

Right (TheText ; Length ( TheText ) - 1

passes all but the very first character to the next recursive call of the function.

The first time through, ABB is compared to AAA and that' not a match.

the next call passes BBBC as TheText so this time around

BBB is compared to BBB and that is a match, terminating the recursion and returning True as the result.

So by stripping off the first character with each recursive call until there is either a match or less than 3 characters, the function recursively scans through the text looking for the pattern of three repeating characters.

PS. Hope "Winston James III" isn't the kind of text you are validating with this funciton!

• ###### 5. Re: 3 or more repeating Characters

hahaha no not for james the lll

im sorry but I still don't get how a case statement can run like a loop.

I understand how it passes all but the very first character in the default result of the case statement. but then shouldnt it output false?

How does it know to continue checking until there is a match or less than 3 characters?

• ###### 6. Re: 3 or more repeating Characters

Recursion can definitely have a bit of "smoke and mirrors" feel.

It's not real easy to explain in simple text, If I were lecturing in a classroom, I'd be sketching out graphic data flow diagrams, but here goes...

Recursion is a kind of "divide and conquier" solution method where you check for the simple, obvious solutions first (Is the text too short to have 3 characters? Does the text repeat for the first three characters?) then it slightly simplifies the problem (Strips off the first character since we know it isn't part of a "repeat 3" at this point) and tries again with the simplified data. It will keep "trying again with simpler data" until one of the simple tests evaluates as true and then the specified value for that clause of the case function is returned back from funciton call to function call until it reaches the original and is returned to the expression where you used this function.

It may help to track it through on paper.

Say you have ABBB as the text that you are originally

The first test is false, There are more than 2 characters.

The 2nd test is false, ABB isn't a "reapeat 3" pattern.

So then the function calls itself--this is the essence of a recursive function or script, the fact that it calls itself. You can imagine at this point that you have two copies of this function, the first copy is using the second copy to get a result. When the second function "copy" produces a result, it will be passed back to the previous "copy" of this function.

So then it sends BBB to a second call to the function.

The first test is still false, but the second test is True, the first 3 letters repeat so True is returned from the second call of this function and no 3rd call is needed. This returns us back to the the last line of the Case function in the first copy, the one with the recursive call, which now evaluates as True--it will evaluate to whatever value the 2nd copy returns, and as this is the last line of the function, this is the value returned to your calcualtion that used it.