How to check that a string contains at least 5 unique characters and at least 7 characters?

I need a check that checks a string containing 5 unique characters and a length of at least 7 characters.

I already tried it with the following regex:

^[a-zA-Z][0-9]{7}$ 

I'm stuck and have no idea how to make sure that the string contains at least 5 unique characters.

+5
source share
3 answers

I don’t think it would be easy to check if you have at least 5 unique characters with regular expression, so I use a different approach.

I check preg_match() that your string contains only characters from this [a-zA-Z0-9] and at least 7 characters long, which I check with quantifier : {7,} .

Then, to make sure that you have> = 5 unique characters, I split your string into an array with str_split() , get all the unique characters of array_unique() and check the count() box if there are> = 5 unique characters, e.g.

 if(preg_match("/^[a-zA-Z0-9]{7,}$/", $input) && count(array_unique(str_split($input))) >= 5) { //good } else { /bad } 

As, from the question, it is unclear whether you want to perform validation in or by adding similar code using Javascript.

 var regex = /^[a-zA-Z0-9]{7,}$/; // Adding method on prototype, so that can be invoked on array Array.prototype.unique = function() { var arr = this; // Cache array // Return the array by removing duplicates return this.filter(function(e, i) { return arr.indexOf(e) === i; }); }; // Binding keyup event on textbox(Demo Purpose) document.getElementById('text').addEventListener('keyup', function(e) { var str = this.value; // Get value this.classList.remove('invalid'); // Remove classes // Check if regex satisfies, and there are unique elements than required if (regex.test(str) && str.split('').unique().length >= 5) { console.log('Valid'); this.classList.add('valid'); // Demo Purpose } else { console.log('Invalid'); this.classList.add('invalid'); // Demo Purpose } }, false); 
 .valid { border: solid 1px green; } .invalid { border: solid 1px red; } 
 <input id="text" type="text" /> 

For completeness only, there will be a solution with a regular expression only:

 ~^ (?=.{7,}) ([az\d])[az\d]* (?!\1)([az\d])[az\d]* (?!\1|\2)([az\d])[az\d]* (?!\1|\2|\3)([az\d])[az\d]* (?!\1|\2|\3|\4)([az\d])[az\d]* $~i 

Simply put:

  • (?=.{7,}) ensures that there must be at least 7 characters from the very beginning to the end of the line
  • Then there are 5 capture groups with a negative look that guarantee that their match cannot be in any other of the 4 capture groups. Thus, this ensures that the string contains at least 5 unique characters.
  • [az\d]* around capture and negative look groups just say there can be all kinds of things around these 5 unique characters.

(Regular expression can still be optimized)

+9
source

You want {6,} for seven or more, I think. You probably need to do something besides the regular expression for "5 or more different"

Or maybe ^ [a-zA-Z0-9] {7,} if you had the wrong brackets.

I don't know php, but here is the algorithm I would use for part 5 diff:

 int seen[256] = {0}; int ndiff = 0; for each char c in string { seen[c]++; } for (int i = 0; i < 256; ++i) { if (seen[i]) ndiff++; } 
0
source

I think you have too many brackets @ "^ [a-zA-Z0-9] {7} $"

-2
source

Source: https://habr.com/ru/post/1239950/


All Articles