[Math] Regex for strings with at least three unique characters

regular expressionsregular-languageregularization

I'm trying to represent some string conditions in terms of regex. One of those conditions I find hard to transform is that the string must have at least three different characters. So is there any plausible regex for that condition? Suppose the alphabet only contains English characters (a-z A-Z).

Thank you,

Best Answer

If your alphabet is $\{a, b, c\}$ only, and you write $A$ for $(a \mid b \mid c)$, then something like $$A^* ( a A^* b A^* c \mid a A^* c A^* b \mid b A^* a A^* c \mid b A^* c A^* a \mid c A^* a A^* b \mid c A^* b A^* a ) A^* $$ will do. If your alphabet had $n$ symbols, this would mean 6 cases for each of the $\binom{n}{3}$ ways of selecting three different symbols.