아래 조건을 충족해야하는 암호 규칙이 있습니다.
다음 중 최소 2 개 :-최소 1 개의 소문자-최소 1 개의 대문자-최소 2 개 (숫자 및 특수 문자)
다음과 같이 정규식을 작성합니다.
String oneLowercaseCharacter = ".*[a-z].*";
String oneUppercaseCharacter = ".*[A-Z].*";
String oneNumber = ".*\\d.*";
String oneSpecialCharacter = ".*[^\\`\\~\\<\\,\\>\\\"\\'\\}\\{\\]\\[\\|\\)\\(\\;\\&\\*\\$\\%\\#\\@\\!\\:\\.\\/\\?\\\\\\+\\=\\-\\_\\ ].*";
String threeNumbersAndCharacters = ".*[0-9\\^\\`\\~\\<\\,\\>\\\"\\'\\}\\{\\]\\[\\|\\)\\(\\;\\&\\*\\$\\%\\#\\@\\!\\:\\.\\/\\?\\\\\\+\\=\\-\\_\\ ].*[0-9\\^\\`\\~\\<\\,\\>\\\"\\'\\}\\{\\]\\[\\|\\)\\(\\;\\&\\*\\$\\%\\#\\@\\!\\:\\.\\/\\?\\\\\\+\\=\\-\\_\\ ].*[0-9\\^\\`\\~\\<\\,\\>\\\"\\'\\}\\{\\]\\[\\|\\)\\(\\;\\&\\*\\$\\%\\#\\@\\!\\:\\.\\/\\?\\\\\\+\\=\\-\\_\\ ].*";
그런 다음 아래와 같은 함수를 빌드합니다.
if ((Pattern.compile(oneLowercaseCharacter).matcher(s).find() && Pattern.compile(oneUppercaseCharacter).matcher(s).find())
|| (Pattern.compile(oneLowercaseCharacter).matcher(s).find()
&& Pattern.compile(oneSpecialCharacter).matcher(s).find()
&& Pattern.compile(oneNumber).matcher(s).find()
&& Pattern.compile(threeNumbersAndCharacters).matcher(s).find())
|| (Pattern.compile(oneUppercaseCharacter).matcher(s).find()
&& Pattern.compile(oneSpecialCharacter).matcher(s).find()
&& Pattern.compile(oneNumber).matcher(s).find()
&& Pattern.compile(threeNumbersAndCharacters).matcher(s).find())) {
//Do my stuff here
}
그러나 예상대로 작동하지 않습니다. 이유는 확실하지 않지만 다른 비밀번호로 테스트하면 결과가 다음과 같이 표시됩니다.
qwerty123 true (예상되지 않음)
qwerty! @ # 거짓
qwerty12. 진실
Qwerty123 참
쿼티 12. 진실
아무도 내가 놓친 곳을 알고 있습니까?
참고 : 이미 stackoverflow를 검색하고 이미 다른 곳을 찾아 위의 코드를 찾았지만 더 이상 진행할 수는 없습니다.
문제는 다음 줄에 있습니다.
String oneSpecialCharacter = ".*[^\\`\\~\\<\\,\\>\\\"\\'\\}\\{\\]\\[\\|\\)\\(\\;\\&\\*\\$\\%\\#\\@\\!\\:\\.\\/\\?\\\\\\+\\=\\-\\_\\ ].*";
문자 ^
는 내부의 첫 번째 위치에서 사용될 때 특별한 의미 ( "아님")를 갖습니다 []
.
이것이 당신이 그것을 탈출해야하는 이유입니다.
String oneSpecialCharacter = ".*[\\^\\`\\~\\<\\,\\>\\\"\\'\\}\\{\\]\\[\\|\\)\\(\\;\\&\\*\\$\\%\\#\\@\\!\\:\\.\\/\\?\\\\\\+\\=\\-\\_\\ ].*";
이제 결과는 다음과 같습니다.
qwerty123 -> false
qwerty!@# -> false
qwerty12. -> true
Qwerty123 -> true
Qwerty12. -> true
의 의미를 강조하는 다른 예 ^
:
// the first character cannot be a
System.out.println(Pattern.compile("[^a]bc").matcher("abc").find()); // false
// the first character cannot be x, y or z, but it can be a
System.out.println(Pattern.compile("[^xyz]bc").matcher("abc").find()); // true
// the first character can be ^ or a
System.out.println(Pattern.compile("[\\^a]bc").matcher("abc").find()); // true
// the first character can be ^, x, y or z, but not a
System.out.println(Pattern.compile("[\\^xyz]bc").matcher("abc").find()); // false
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다