Study/java

[Java] 비밀번호 체크 정규식 모음(영문, 숫자, 특수문자 포함)

 

Java로 비밀번호 체크 시 많이 사용되는 정규식에 대해 설명드리겠습니다.

 

영문 대소문자 1개 이상 포함 여부 정규식

대문자 포함 여부 체크

비밀번호에 대문자가 포함되었는지 체크하는 정규식입니다.

String includeUpperLetterPassword = "Abcdefg123";
String notIncludeUpperLetterPassword = "abcdefg123"; // 대문자가 없는 비밀번호
String includeUpperLetterRegex = "^(?=.*[A-Z]).+$";

System.out.println("includeUpperLetter - " + includeUpperLetterPassword + " : " + includeUpperLetterPassword.matches(includeUpperLetterRegex));
System.out.println("notIncludeUpperLetter - " + notIncludeUpperLetterPassword + " : " + notIncludeUpperLetterPassword.matches(includeUpperLetterRegex));

대문자 포함여부 정규식 체크 결과
대문자 포함여부 정규식 체크 결과

 

대소문자 1개 이상 포함 여부 체크

대소문자가 1개 이상 포함되어있는지 체크하는 정규식입니다.

String includeUpperLetterPassword = "Abcdefg123";
String notIncludeUpperLetterPassword = "abcdefg123";

String includeUpperAndLowerLetterRegex = "^(?=.*[A-Z])(?=.*[a-z]).+$";

System.out.println("includeUpperAndLowerLetter - " + includeUpperLetterPassword + " : " + includeUpperLetterPassword.matches(includeUpperAndLowerLetterRegex));
System.out.println("includeUpperAndLowerLetter - " + notIncludeUpperLetterPassword + " : " + notIncludeUpperLetterPassword.matches(includeUpperAndLowerLetterRegex));

대소문자 1개 이상 포함 여부 체크 결과
대소문자 1개 이상 포함 여부 체크 결과

 

숫자 포함 여부 정규식

숫자가 포함되었는지 체크하는 정규식입니다.

String includeNumberPassword = "Abcdefg123";
String notIncludeNumberPassword = "Abcdefg";

String includeNumberRegex = "^(?=.*[0-9]).+$";

System.out.println("includeNumber - " + includeUpperLetterPassword + " : " + includeUpperLetterPassword.matches(includeNumberRegex));
System.out.println("notIncludeNumber - " + notIncludeNumberPassword + " : " + notIncludeNumberPassword.matches(includeNumberRegex));

숫자 포함 여부 체크 결과
숫자 포함 여부 체크 결과

 

 

 

 

 

 

 

특수문자 1개 이상 포함 여부 정규식

특수문자가 1개 이상 포함되어있는지 체크하는 정규식입니다.

String includeSpecialLetterPassword = "Abcdefg123!";
String notIncludeSpecialLetterPassword = "Abcdefg123";

String includeSpecialLetterRegex = "^(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?]).+$";

System.out.println("includeNumber - " + includeUpperLetterPassword + " : " + includeUpperLetterPassword.matches(includeNumberRegex));
System.out.println("notIncludeNumber - " + notIncludeNumberPassword + " : " + notIncludeNumberPassword.matches(includeNumberRegex));

특수문자 포함 여부 체크 결과
특수문자 포함 여부 체크 결과

 

영문 + 숫자 + 특수문자 포함 여부 정규식

영문 대소문자 1개 이상 포함, 숫자 포함, 특수문자 포함여부를 체크하는 정규식입니다.

String includeUpperAndNumberAndSpecialLetterPassword = "Abcdefg123!";
String notIncludeUpperLetterPassword = "abcdefg123";
String notIncludeNumberPassword = "Abcdefg";
String notIncludeSpecialLetterPassword = "Abcdefg123";

String includeUpperAndLowerAndNumberAndSpecialLetterRegex = "^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?]).+$";

System.out.println("includeUpperAndLowerAndNumberAndSpecialLetter - " + includeUpperAndNumberAndSpecialLetterPassword + " : " + includeUpperAndNumberAndSpecialLetterPassword.matches(includeUpperAndLowerAndNumberAndSpecialLetterRegex));
System.out.println("notIncludeUpperLetter - " + notIncludeUpperLetterPassword + " : " + notIncludeUpperLetterPassword.matches(includeUpperAndLowerAndNumberAndSpecialLetterRegex));
System.out.println("notIncludeNumber - " + notIncludeNumberPassword + " : " + notIncludeNumberPassword.matches(includeUpperAndLowerAndNumberAndSpecialLetterRegex));
System.out.println("notIncludeSpecialLetter - " + notIncludeSpecialLetterPassword + " : " + notIncludeSpecialLetterPassword.matches(includeUpperAndLowerAndNumberAndSpecialLetterRegex));

대소문자, 숫자, 특수문자 포함 여부 체크 결과
대소문자, 숫자, 특수문자 포함 여부 체크 결과