-5

I have been told to use regex instead of isUpperCase(), isLowerCase(), etc... but I have no clue on how use regex.

This is what I need to check:

length min = 8 characters
length max = 20 characters
must contain at least one lower case character (a-z)
must contain at least one upper case character (A-Z)
must contain at least one number (0-9)
must contain at least one special character

Thanks.

JohnLocke
  • 23
  • 5

4 Answers4

1

If you want to learn, how to use regex, I recommend the following

  • read your lecture notes
  • read a Java book (especially the chapters about "Pattern" and "regular expressions")
  • read the Java documentation, e.g. about Pattern
mmehl
  • 234
  • 1
  • 6
0

Regex of upper case is [A-Z] and for lower case is [a-z] Both tests for at at least ONE occurence of given char group.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
0

Here is an example from Javascript. Java will be similar:

var password = '0Aa!.......'
const regexp = /[0-9]?[a-z]?[A-Z]?[^0-9A-Za-z]?/;
var isValid = (password.length >= 8 && password.length <= 20) && regexp.test(password);

This tests that there are at least one number, then one capital, then one lowercase, then one that isn't a number or letter. It demonstrates the concept, not the exact solution.

I agree with others that you should dig in to the reading more. Regular expressions and Java are well documented.

Chris Rouffer
  • 743
  • 5
  • 14
  • Wouldn't this match "AAAAAAAA"? Your example requires a very specific order. I don't think this is possible without lookaheads – quackenator May 09 '17 at 20:39
  • You are correct, quackenator. I was trying to demonstrate how regexes are used, not what the exact solution is. The question was getting down-voted, but I wanted to give the author a starting point to do more reading. That's why I didn't do the answer in Java (which he was looking for). – Chris Rouffer May 09 '17 at 21:26
0

Note: I haven't tested this in Java, but this does work with the pcre engine.

^(?=.{8,20}$)(?=[^A-Z]*?[A-Z])(?=[^a-z]*?[a-z])(?=[^0-9]*?[0-9])(?=[^!@#$%^&*]*?[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]+$

Broken down:

    ^(?=.{8,20}$)               //this matches 8 to 20 characters, inclusive (positive lookahead)
    (?=[^A-Z]*?[A-Z])           //this matches if one uppercase letter is present (positive lookahead)
    (?=[^a-z]*?[a-z])           //this matches if one lowercase letter is present (positive lookahead)
    (?=[^0-9]*?[0-9])           //this matches if one digit is present (positive lookahead)
    (?=[^!@#$%^&*]*?[!@#$%^&*]) //this matches if one special character is present (positive lookahead)
    [a-zA-Z0-9!@#$%^&*]+$       //this matches if the enclosed characters are present

This may be a bit over the top for what you want but I have tested it here: https://regex101.com/r/P0J4X8/1

Also, the lazy evaluators may be unnecessary in the lookaheads

  • Thank you very much. I'm going to test this in Java. May I ask you how the regex would become with this additional check ("must NOT contain one or more identical consecutive characters")? – JohnLocke May 10 '17 at 12:41
  • I've tried the following string: ".getInfo(0)$%@;" but it didn't work. – JohnLocke May 10 '17 at 12:50
  • I would look into negative lookaheads, capturing groups, and repeating a capturing group. – quackenator May 10 '17 at 17:33