Regular expression password pattern
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,15}) |
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,15})
This regular expression refers to a pattern with at least one digit, one upper case letter, one lower case letter and one special symbol (“@#
Example
package com.w3schools;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
private static final String PATTERN =
"((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,15})";
public static void main(String args[]){
List<String> values = new ArrayList<String>();
values.add("jaisingh1A@");
values.add("Sjai12$");
values.add("jaiA@");
values.add("jai*");
Pattern pattern = Pattern.compile(PATTERN);
for (String value : values){
Matcher matcher = pattern.matcher(value);
if(matcher.matches()){
System.out.println("Password "+ value +" is valid");
}else{
System.out.println("Password "+ value +" is invalid");
}
}
}
} |
package com.w3schools;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
private static final String PATTERN =
"((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,15})";
public static void main(String args[]){
List<String> values = new ArrayList<String>();
values.add("jaisingh1A@");
values.add("Sjai12$");
values.add("jaiA@");
values.add("jai*");
Pattern pattern = Pattern.compile(PATTERN);
for (String value : values){
Matcher matcher = pattern.matcher(value);
if(matcher.matches()){
System.out.println("Password "+ value +" is valid");
}else{
System.out.println("Password "+ value +" is invalid");
}
}
}
}
Output
Password jaisingh1A@ is valid
Password Sjai12$ is valid
Password jaiA@ is invalid
Password jai* is invalid |
Password jaisingh1A@ is valid
Password Sjai12$ is valid
Password jaiA@ is invalid
Password jai* is invalid