Regex use each letter only once?

Is it possible to create a PCRE-style regular expression that matches only each letter in the list only once?

For example, if you have the letters "lrsa" and you are trying to match a list of words with:

^[lrsa]*m[lrsa]*$

you will correspond to "lams" (valid), but also to "lamas" (not valid for our purposes because you have only one "a"). If your letter was "lrsaa", you would need to combine "lam".

Is this possible with regular expressions, or should I handle it programmatically?

+5
source share
1 answer

You can use a negative forecast ahead:

^(?!.*?(.).*?\1)[lrsa]*m[lrsa]*$

,

+3

All Articles