0

I have a this regex;

("(?=.*[a-z]).*") ("(?=.*[0-9]).*") ("(?=.*[A-Z]).*") ("(?=.*[!@#$%&*()_+=|<>?{}\\[\\]~-]).*")

that checks a password with requirements: length =8, then three of the following - a lowerCase, an upperCase, a digit, special character. 3 of the above 4 + length of 8 is required.

What I have works until there is a space in the password, then it prints the wrong message. In other-words, how do I include whitespace in my list of special characters, thanks!

RoiboisTx
  • 33
  • 5
  • 1
    Do you really need to do that in one regex ? The code will be cleaner with multiple checks. Performance is not likely to be an issue on this use case. If it is hard to write, it will be even harder to read for you future colleagues. – Arnaud Denoyelle Feb 28 '19 at 15:43
  • I meant, it should have at least three of these(a lowerCase, an upperCase, a digit, special character). I used multiple checks, yet when there is a space it returns an error – RoiboisTx Feb 28 '19 at 16:08
  • 1
    You are trying the check something you find difficult to express as a regular expression: think about it. – greybeard Feb 28 '19 at 16:43
  • What do you mean by _special character_? What do you mean by _it breaks_? – Armali Mar 01 '19 at 13:14
  • By special character I meant non numbers and digits. By breaking I meant, a password could be valid, yet the program prints invalid, I have this particularly with the space, let me know if you know away around this, thanks – RoiboisTx Mar 02 '19 at 09:58
  • You should give [Reference - Password Validation](https://stackoverflow.com/q/48345922/3600709) a good read. – ctwheels Jun 19 '19 at 14:00

1 Answers1

0

You can try this out:

String password = "pA55w$rd";

int counter = 0;

if(password.length() >= 8)
{
    Pattern pat = Pattern.compile(".*[a-z].*"); // Lowercase
    Matcher m = pat.matcher(password);
    if(m.find()) counter++;
    pat = Pattern.compile(".*[0-9].*"); // Digit
    m = pat.matcher(password);
    if(m.find()) counter++;
    pat = Pattern.compile(".*[A-Z].*"); // Uppercase
    m = pat.matcher(password);
    if(m.find()) counter++;
    pat = Pattern.compile(".*\\W.*"); // Special Character
    m = pat.matcher(password);
    if(m.find()) counter++;

    if(counter == 3 || counter == 4)
    {
        System.out.println("VALID PASSWORD!");
    }
    else
    {
        System.out.println("INVALID PASSWORD!");
    }
}
else
{
    System.out.println("INVALID PASSWORD!");
}

There are two cases: either it matches the length required, or not.

If it does match the length, it checks each of the 4 cases once, and increments a counter every time it does. Since you want it to match 3 or 4 of the cases, I put an if-else case over there.

Robo Mop
  • 3,485
  • 1
  • 10
  • 23
  • Mob, thanks for the code. it works well until the test engine enters 1234_67a. Which is supposed to be valid, but the code prints invalid, something to do the special character I think, please look into it. Btw, the counting thing, I found to be pretty smart, I'm new to java and didn't think of that approach. – RoiboisTx Mar 02 '19 at 09:53
  • @RoiboisTx: `\W` excludes `_` that is considered as a "word" character. Replace it with `[^a-zA-Z0-9]`. – Toto Mar 02 '19 at 14:46