Reputation: 1753
Is there a way how to define regular expression in which will match only if there are at least N unique characters?
Example: (N = 3)
aba => no match
abc => match
abbc => match
ab => no match
abcd => match
Upvotes: 9
Views: 2147
Reputation: 70933
EDITED
/(?:(.)(?!.*?\1).*){3}/
Change 3 with the desired value of unique characters
PREEDIT - Just for documentation and to keep coherence for the comments, this was the original answer posted
/^(?:(.)(?!.*\1.*$)){3}$/
No, it does not what the OP needed, i misunderstood the problem. This regexp tests that the string is formed only by the indicated number of unique characters.
Upvotes: 0
Reputation: 785226
These problems are pretty tricky to do using regex
.
SInce question is tagged as regex
you can try this lookahead
based regex:
(.).*?((?!.*?\1).).*?((?!.*?\2).)
Online Demo: http://regex101.com/r/dH1rP4
It doesn't match:
It matches:
Upvotes: 2
Reputation: 2566
Not really, this is not a regex problem.
A much easier solution would be to use a Set like HashSet(T)
Split the string to characters and add each one to the set.
Count the number of elements in the set.
Upvotes: 5