tags:

views:

88

answers:

4

Hi,
I'm trying to figure out how to write a regex that can detect if in my string, any character is repeated more than five times consecutively? For example it wouldn't detect "hello", but it would detect "helloooooooooo".

Any ideas?
Thanks

Edit: Sorry, to clarify, I need it to detect the same character repeated more than five times, not any sequence of five characters. And I also need it to work with any charter, not just "o" like in my example. ".{5,}" is no good because it just detects any sequence of any five characters, not the same character.

+3  A: 

Correction, should be (.)\1{5,}, I believe. My mistake. This gets you:

(.)  #Any character
\1   #The character captured by (.)
{5,} #At least 5 more repetitions (total of at least 6)

You can also restrict it to letters by using (\w)\1{5,} or ([a-zA-Z])\1{5,}

eldarerathis
This will also match *"hello"*, *"goodbye"*, or any other string longer than four characters.
LukeH
@LukeH: Definitely spaced on that. Thanks.
eldarerathis
This will work, but note that "." matches every single character except Linebreaks. This includes whitepace, numbers, etc
Powertieke
@Powertieke: Yes, well I'm not sure exactly what the OP means by "characters" so I just gave a couple of options.
eldarerathis
Oops, missed that :). +1 for being thorough!
Powertieke
+9  A: 

This should do it

(\w)\1{5,}
  • (\w) match any character and put it in the first group
  • \1{5,} check that the first group match at least 5 times.

Usage :

$input = 'helloooooooooo';
if (preg_match('/(\w)\1{5,}/', $input)) {
 # Successful match
} else {
 # Match attempt failed
}
madgnome
You're totally right, I've fixed my answer to match only more than 5 repetitions.
madgnome
Perfect, thanks!
Jack Sleight
+1 for fixing it.
eldarerathis
A: 

You can use the regex:

(.)\1{5,}

Explanation:

  • . : Meta char that matches any char.
  • () : Are used for grouping and remembering the matched single char.
  • \1 : back reference to the single char that was remembered in prev step.
  • {5,} : Quantifier for 5 or more

and in PHP you can use it as:

$input = 'helloooooooooo';
if(preg_match('/(.)\1{5,}/',$input,$matches)) {
  echo "Found repeating char $matches[1] in $input";
}

Output:

Found repeating char o in helloooooooooo
codaddict
A: 

Yep.

(.)\1+

This will match repeated sequences of any character.

The \1 looks at the contents of the first set of brackets. (so if you have more complex regex, you'd need to adjust it to the correct number so it picks up the right set of brackets).

If you need to specify, say more than three of them:

(.)\1{3,}

The \1 syntax is quite powerful -- eg You can also use it elsewhere in your regex to search for the same character appearing in different places in your search string.

Spudley