2

I am trying to make a regular expression that will match all words that have a letter that repeats at least an arbitrary number of times.

For example, if I want to match words that have a letter that repeats at least 3 times, I would want to match words like

applepie banana insidious

I want to be able to change the number of repeats I'm looking for by just changing one number in my expression, so expressions that only work for a certain number of repeats are not what I'm looking for.

Currently, this is what I'm using

^(?=.*(.))(?=(.*\1){4}).*$

Where 4 is the number of repeats, a number that I can change to whatever number of repeats I'm looking for.

The above regular expression appears to work, but using a lookahead just so I can use a capturing group seems very unwieldy, and so I'm looking for a better way to solve this problem.

Sun T
  • 53
  • 6
  • Boderline duplicate of [this one](http://stackoverflow.com/questions/26515936/find-words-without-repeated-characters-using-php-regex/26519014#26519014) –  Oct 27 '14 at 07:05

2 Answers2

3

This will eliminate one lookahead:

\b(?=\w*(\w)(\w*\1){2})\w*

Start of word, then any number of word-characters such that they consist of any number of word characters, a particular word character, and then any number of characters and that character again, repeated at least twice.

For four repetitions, use {3} (for n repetitions, use one less).

Also, feel free to replace \b... with ^...$ as you were doing if you meant to match whole lines and not words in text.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • +1 I did my solution without looking at the answers. `\b(?=\w*(\w)(?:\w*\1){2})\w+` good thing I saw yours before I posted. –  Oct 27 '14 at 07:00
1

You can use this regex:

\b\w*?(\w)(?=(?:\w*?\1){2})\w*\b

RegEx Demo

Where 2 is n-1 for n repetitions you're trying to find in a complete word.

anubhava
  • 761,203
  • 64
  • 569
  • 643