2
^([rat])(?!\g1)([rat])(?!(\g2|\g1))[rat]$

If possible, I want to get rid of the repetition of [rat] in my regex. May be substitute it for something like \c or so.

revo
  • 47,783
  • 14
  • 74
  • 117

1 Answers1

1

You are matching unique characters from character class. Simply saying input string should not contain a second occurrence of defined characters. Just check if the opposite happens:

([rat]).*\1

Or all in one go, validating if string follows ^[rat]{3}$ rule as well:

^(?!.*?(.).*\1)[rat]{3}$

Live demo

Breakdown:

  • ^ Assert beginning of input string
  • (?! Construct a negative lookahead
    • .*?(.).*\1 Look for duplicate characters
  • ) End of negative lookahead
  • [rat]{3} Match a 3 char long string including defined chars
  • $ End of input string
revo
  • 47,783
  • 14
  • 74
  • 117