Find a group of chars with RegEx

Is there a simple way to see if a string has groups of chars that contain x time the same chars.

For example:
Lets say I need to know if a string contains a group of at least 4 of the same characters next to each other.

“A40000GYHHiPYuU458fFFPfG” -> this should return true because the strings has a group of chars with four 0000 in it.
“AZERTYUIOPMLKJHGFDSQSSDFGGSG” -> this should return false because there are no groups of four chars with the same chars.
“AQWXSZEDCVFRTTTTTTTGBNHYUJ?.KIO” -> this should return true because it contains “TTTTTTT”
“AZAZAZAAZAZAZAZAZAZAZAZAZAZAZAZ” -> should return false

Yes, Regular Expressions :wink:

Function Countem(x as string, l as integer) As Boolean

[code] ’ x=string to search
’ l=number of repeated characters

Dim i as integer
Dim j as integer
Dim y as string

If l < 2 then
Msgbox “Use instr, stupid!”
return false
end if

j=1
y=""
For i=1 to len(x)
If y=mid(x,i,1) then
j=j+1
else
y=mid(x,i,1)
j=1
end if
if j >=l then
return true
end if
next
return false
[/code]

I’m sure the much cleverer people here could find a better way though.

That probably is working. Thanks.
But I was more looking for a simple way like using Regex :slight_smile:

Where is the Regex goeroe Kem? :wink:

Sorry!

I’m not on a computer, so not tested in Xojo:

RegEx: /([A-Z0-9])\1{3}/g

([A-Z0-9]) capital letters and digits
\1 backreference to capital letters and digits
{3} backreference repeated 3 times

No need to say sorry! Your code is fine but was referring to a solution with Regex. Sorry if this wasn’t clear. :slight_smile:

And thanks for taking the time to share your solution !

[quote=258114:@Eli Ott]I’m not on a computer, so not tested in Xojo:

RegEx: /([A-Z0-9])\1{3}/g

([A-Z0-9]) capital letters and digits
\1 backreference to capital letters and digits
{3} backreference repeated 3 times[/quote]

Thanks Eli. Will check later if this works.
RegEx … It’s still a pain in the @ss for me :slight_smile:

To match any digit 3 or more times

[code]dim rx as new RegEx
rx.SearchPattern = “(?Umi-s)([0-9])\1{3,}”

dim rxOptions as RegExOptions = rx.Options
rxOptions.LineEndType = 4

dim match as RegExMatch = rx.Search( sourceText )[/code]
“A40000GYHHiPYuU458fFFPfG”

To match any repeated char 3 or more times

[code]dim rx as new RegEx
rx.SearchPattern = “(?Umi-s)([a-zA-Z])\1{5,}”

dim rxOptions as RegExOptions = rx.Options
rxOptions.LineEndType = 4

dim match as RegExMatch = rx.Search( sourceText )[/code]

“AQWXSZEDCVFRTTTTTTTGBNHYUJ?.KIO”

I cant edit to my previous post.
Change the {3,} for more accurate if you want
{2,} two or more
{1,3} between one & three
{2,}? match as few as possible

Sorry, both Eli and Loannis code doesn’t work.

I make a simple example maybe help you?
Regex

[quote=258211:@Loannis Kolliageorgas]I make a simple example maybe help you?
[/quote]

Given that there is punctuation in the example, you probably need to cater for hex values &h21 to &h7F at least.

Thanks Loannis ! Works great - not sure what I did wrong (as usual) :slight_smile:

i am glad that works.

Both versions should work just fine. One caveat: Unless you turn on case-sensitivity either through RegEx.Options or a switch, Eli’s version will match both upper and lowercase letters. Both will match “aAaA”, for example. If you don’t want that. set rx.Options.CaseSensitive = False.

Neither version will match high-ascii, unicode characters, but I’m not sure that’s a concern. If it is, try this:

([\\pL\\pN])\\g1{3,}

This will match any letter or number that repeats at least 4 times in succession. The entire code would look something like this:

dim rx as new RegEx
rx.SearchPattern = "([\\pL\\pN])\\g1{3,}"

if rx.Search( myString ) isa RegExMatch then
  //
  // Has at least 4 characters in succession
  //
end if

This assumes that you don’t actually have to know what those characters are. If you do, then:

dim rx as new RegEx
rx.SearchPattern = "([\\pL\\pN])\\g1{3,}"

dim match as RegExMatch = rx.Search( myString )
while match isa RegExMatch
  dim char as string = match.SubExpressionString( 1 ) // The matching character
  //
  // Do something with the character
  //

  rx.Search
wend

One thing: Would it be possible to have one RegEx pattern that checks both numbers and letters?

yes the pattern from @Kem Tekinay :slight_smile:
“(?Umi-s)([\pL\pN])\g1{3,}”

Really, although RegEx is build out of logic, it isn’t very logical to me. :slight_smile:

Thanks to all for helping out!

[quote=258229:@Christoph De Vocht]Really, although RegEx is build out of logic, it isn’t very logical to me. :slight_smile:

Thanks to all for helping out![/quote]
Is no that hard if you have the app from @Kem Tekinay , is really nice app :wink: