Pavel Sem
Pavel Sem

Reputation: 1753

Regular expression to match only if there are N unique characters

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

Answers (3)

MC ND
MC ND

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

anubhava
anubhava

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).)
  • First this matches any character and captures that in group #1
  • Then it searches in the string any character which is not group #1 and captures that in group #2
  • Finally it further searches in the string any character which is not group #2

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

It doesn't match:

  1. aba
  2. adaaaaaa
  3. aaaabbbb

It matches:

  1. abc
  2. adaac
  3. abbc
  4. adaaac
  5. 11112222220

Upvotes: 2

Tristan Burnside
Tristan Burnside

Reputation: 2566

Not really, this is not a regex problem.

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

  1. Split the string to characters and add each one to the set.

  2. Count the number of elements in the set.

Upvotes: 5

Related Questions