Sitansu
Sitansu

Reputation: 3327

Regular Expression in Java for Password Pattern

The regex not working for "At least one Alphabets,At least one Digits and At least one Special Characters" and "At least one Digits and At least one Special Characters"

For example :

String passwordpattern="A9009"; //Not working for  pattern3

Note:It should check atleast one Alphabets, Digits and Special Characters

and

String passwordpattern="A3566523"; //Not working for  pattern4

Note:It should check at least one Special character and at least Digit

    //Alphabets, Digits and Special Characters
            String pattern3 = "[^\\\\w\\\\d]*(([0-9]+.*[A-Za-z]+.*[!#%&'()*+,-:;<=>?@}{]+.*)|[A-Za-z]+.*[0-9]+.*[!#%&'()*+,-:;<=>?@}{]+.*|[!#%&'()*+,-:;<=>?@}{]+.*[A-Za-z]+.*[0-9]+.*|[!#%&'()*+,-:;<=>?@}{]+.*[0-9]+.*[A-Za-z]+.*|[A-Za-z]+.*[!#%&'()*+,-:;<=>?@}{]+.*[0-9]+.*|[0-9]+.*[!#%&'()*+,-:;<=>?@}{]+.*[A-Za-z]+.*)";

   //Digits and Special Characters
  String pattern4 = "([^\\\\w\\\\d]*(([!#%&'()*+,-:;<=>?@}{]+.*[0-9]+.*)|[0-9]+.*([!#%&'()*+,-:;<=>?@]+.*)))";

Upvotes: 1

Views: 95

Answers (2)

Matthew
Matthew

Reputation: 1943

This regex pattern will do what you want - match only if there is at least one alphabetic letter, one digit and one special character in a given string input:

^(?=.)[a-zA-Z]+[0-9]+[^\w]+[^\s]+

Upvotes: 0

anubhava
anubhava

Reputation: 786349

For these type of assertions it is better to use lookahead assertions.

"At least one Alphabets,At least one Digits and At least one Special Characters"

^(?=.*\pL)(?=.*\d)(?=.*\W).+$

\pL matches any unicode letter, \d matches any digit and \W any non-word character.

at least one Special character and at least Digit

^(?=.*\d)(?=.*\W).+$

Note that while using matches method there is no need to use anchors.

Upvotes: 4

Related Questions