Regular expression to match only if there are N unique characters

Is there a way to define a regex that matches only if there are at least N unique characters?

Example: (N = 3)

aba => no match abc => match abbc => match ab => no match abcd => match 
+7
regex
source share
3 answers

Not really, this is not a regex problem.

It would be much easier to use a Set like HashSet (T)

  • Divide the string into characters and add them to the set.

  • Count the number of elements in the set.

+4
source share

These problems are pretty hard to do with regex .

SInce the question is marked as regex , you can try this lookahead :

 (.).*?((?!.*?\1).).*?((?!.*?\2).) 
  • First it matches any character and commits it to group # 1
  • Then he searches the string for any character that is not group # 1, and captures this in group # 2
  • Finally, he continues to search the string for any character that is not group # 2

Online Demo: http://regex101.com/r/dH1rP4

It does not match:

  • ABA
  • adaaaaaa
  • aaaabbbb

It corresponds to:

  • Abc
  • adaac
  • Abbv
  • adaaac
  • 11112222220
+1
source share

edited

 /(?:(.)(?!.*?\1).*){3}/ 

Change 3 with the desired unique character value

PREEDIT - for documentation purposes only and to ensure consistency of comments, this was the original answer posted

 /^(?:(.)(?!.*\1.*$)){3}$/ 

No, this is not what the OP needs, I misunderstood the problem. This regular expression verifies that the string is only formed by the specified number of unique characters.

0
source share

All Articles