I’m writing a function that returns true if the argument, which is a string, contains at least 2 characters from at least 2 categories:
- lower case letter
- upper case letter
- digit grammar
- character/everything else
EXAMPLE:
abAB => true
aB => false
ab12 => true
1ab2 => true
asdfasdf1 => false
I’m working on a regular expression that does this but am having trouble. I’ve also considered breaking up the regular expressions into multiple if statements and checking individually if at least 2 characters from each group are contained in the string.
For example:
comprised = 0
if(string contains *[0-9]*[0-9])
comprised = comprised+1
if(string contains *[a-b]*[a-b])
comprised = comprised+1
if(string contains *[A-Z]*[A-Z])
comprised = comprised+1
if(string contains *[^0-9a-zA-Z]*[^0-9a-zA-Z])
comprised = comprised+1
if comprised >= 2
return true
else return false
There is really no need for regex in this situation. The java code below shows an alternative way of solving it.
Output:
Site where you can test the code: fiddle