Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match only if there are N unique characters

Tags:

.net

regex

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
like image 394
mybrave Avatar asked Dec 09 '13 10:12

mybrave


2 Answers

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.

like image 120
Tristan Burnside Avatar answered Oct 27 '22 02:10

Tristan Burnside


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
like image 27
anubhava Avatar answered Oct 27 '22 02:10

anubhava