Take the 2-minute tour ×

I have to check user input to make sure name, last name (etc...) are entered correctly and are valid. I have to build a regexp that check if a user enters repeated letters in the first name , last name etc...


  • AAAron = bad because of the 3 A's
  • AAron = good
  • Hannah = Good
  • Hannnah = bad because of the 3 N's

Is there a PHP regular expression to catch these cases? (I have a basic regexp knowledge but this is too much for me)

EDIT: This should Allow numbers as well: David 3 or III


share|improve this question
Who are you to validate someone's name on spelling? :) –  Wesley van Opdorp Oct 25 '11 at 12:44
Hope you are not planning to go international. –  Avada Kedavra Oct 25 '11 at 12:53
@WesleyvanOpdorp someone that got a project which specific requirement. Have you heard about the name: AAAAAAAAron? very popular these days –  Tech4Wilco Oct 25 '11 at 12:59
What about my friend LLywellyn? Is he not allowed to join your site? –  DaveRandom Oct 25 '11 at 12:59
Downvoting to -3 seems a bit harsh, this may not be the best idea in the world but it's a valid and clearly phrased question. I don't think downvoting on SO is meant for when you disagree with the idea. –  mikel Oct 25 '11 at 13:04

2 Answers 2

up vote 13 down vote accepted

You can use back reference for that purpose.

preg_match('/(\w)(\1+)/', $subject, $matches);

the \1 means repeat the first capture so in that case \w.

In the case of your example, I don't think using regular expression would be the best solution, why don't you just count the number of instance of any characters?


$charCountArray = array();
foreach ($name as $char) {

back reference is an advanced feature, luckily the PCRE functions supports it.

Note: preg_match would match only one sequence, if you need to know all the matches please use preg_match_all

share|improve this answer
very nice thanks –  Tech4Wilco Oct 25 '11 at 13:17

Try this regular expression:

share|improve this answer

Your Answer


