There are online generators that will make a permutations list that are distinct using
letters. I used this tool here https://www.dcode.fr/permutations-generator and is descent.
Given that list I added an OR symbol |
between each word which made it into a regex.
Taking this one step further I made it into a full regex trie
which is what makes
all the search engines and word completion editors so fast.
There may be regex trie generators out there that can assist in this process.
This is the fastest way to go.
I used Regexformat 9 to create this trie and there are others. This one uses a refactor engine to compact it and make it faster.
The more distinct letters and the number of letters can add up where you need a tool
to construct these.
Example: ABCDE
generates 120 unique permutations. After constructing a trie it becomes:
A(?:B(?:C(?:DE|ED)|D(?:CE|EC)|E(?:CD|DC))|C(?:B(?:DE|ED)|D(?:BE|EB)|E(?:BD|DB))|D(?:C(?:BE|EB)|B(?:CE|EC)|E(?:BC|CB))|E(?:B(?:CD|DC)|C(?:BD|DB)|D(?:BC|CB)))|B(?:A(?:C(?:DE|ED)|D(?:CE|EC)|E(?:CD|DC))|C(?:A(?:DE|ED)|D(?:AE|EA)|E(?:AD|DA))|D(?:C(?:AE|EA)|A(?:CE|EC)|E(?:AC|CA))|E(?:A(?:CD|DC)|C(?:AD|DA)|D(?:AC|CA)))|C(?:A(?:B(?:DE|ED)|D(?:BE|EB)|E(?:BD|DB))|B(?:A(?:DE|ED)|D(?:AE|EA)|E(?:AD|DA))|D(?:B(?:AE|EA)|A(?:BE|EB)|E(?:AB|BA))|E(?:B(?:AD|DA)|A(?:BD|DB)|D(?:AB|BA)))|D(?:C(?:B(?:AE|EA)|A(?:BE|EB)|E(?:AB|BA))|B(?:C(?:AE|EA)|A(?:CE|EC)|E(?:AC|CA))|A(?:C(?:BE|EB)|B(?:CE|EC)|E(?:BC|CB))|E(?:A(?:BC|CB)|B(?:AC|CA)|C(?:AB|BA)))|E(?:A(?:B(?:CD|DC)|C(?:BD|DB)|D(?:BC|CB))|B(?:A(?:CD|DC)|C(?:AD|DA)|D(?:AC|CA))|C(?:B(?:AD|DA)|A(?:BD|DB)|D(?:AB|BA))|D(?:ABC|B(?:AC|CA)|C(?:AB|BA)|ACB))
which is extremely fast.
Note that there is another way to do this using regex without using a trie.
It uses a little known way to count in regex.
It is a combination of a conditional (?(cond)yes|no)
and a group quantity. (?:){5}
Since the problem requires 5 characters, three C and two T, using 5 capture variables as
flags, each distinct and one for each letter given a total count of letters .
A small regex can be crafted. Each time a letter is found, a capture variable flag gets set.
If no capture flags are available, the match fails.
Like this: (?:C(?(3)(?(2)(?(1)(?!)|())|())|())|T(?(5)(?(4)(?!)|())|())){5}
https://regex101.com/r/PEnzkR/1
Conditionals are supported by the JGsoft engine, Perl, PCRE,
Python, and .NET. Ruby supports them starting with version
2.0. Languages such as Delphi, PHP, and R that have regex
features based on PCRE also support conditionals.
Note that the performance is slower than making a full blown regex trie.
It is however limited by the number of capture groups, but any more than 10 is
highly unlikely needed.