3

I'm trying to check if a word have three or more repeating 'two letters pare', But the expressing returns true only if the same letter is repeated. Why?

(([a-z])([^\1]))\1\2{2,} 
    ^      ^     ^ ^  ^
    1      2     3 4  5

1) any letter (captured set \1)
2) any character that is not set 1 (captured set \2)
3) captured \1 again
4) captured \2 again
5) at least twice

words that should return TRUE: asasasassafasf, ereeeerererere, dddddtrtrtrtruuuuuuuu

words that should return FALSE: dddddddd, rrrrrrrrrrlkajf, fffffffssssssytytfffffff

Sdgsdg Asgasgf
  • 205
  • 2
  • 6
  • 1
    Backreferences don't work within character classes (after all they might be comprised of more than one symbol). See [General approach for (equivalent of) "backreferences within character class"?](http://stackoverflow.com/q/18242119) for the lookahead alternative. – mario Jul 19 '14 at 12:25
  • Few examples will greatly help to clarify your problem. – anubhava Jul 19 '14 at 12:26
  • @anubhava asasasas bhbhbhbhbh blaytytytyt... – Sdgsdg Asgasgf Jul 19 '14 at 12:28
  • You really need to give us examples. There are several interpretations to `have three or more repeating 'two letters pairs'` – zx81 Jul 19 '14 at 12:28

2 Answers2

3

You can solve this using a negative lookahead assertion:

([a-z])((?!\1)[a-z])(?:\1\2){2,}

Test it live on regex101.com.

Explanation:

([a-z])  # Match and capture a single ASCII letter in group 1
(        # Match and capture...
 (?!\1)  # unless it's the same letter as the one captured before
 [a-z]   # a single ASCII letter in group 2
)        # End of group 2
(?:\1\2) # Match those two letters
{2,}     # two or more times
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • As you pointed out in a comment in my answer, the two letters must be different, +1 for implementing that. :) – zx81 Jul 19 '14 at 12:40
2

Based on your short description following regex should work for you:

(([a-z])((?!\2)[a-z]))\1{2}

Regex Demo

  • ([a-z]) - matches letters a to z and capture them in a group
  • \2 - Back reference to group #2
  • (?!\2)[a-z]) - matches letters a to z that is NOT group #2
  • \1{2} 2 instances of back reference #1

OR using relative back reference numbering:

(([a-z])(?!\g{-1})[a-z])\1{2}

In short this regex matches 3 consecutive occurrences of a letter pair of different characters.

anubhava
  • 761,203
  • 64
  • 569
  • 643