How can I use a method like this
private boolean respectPattern(String password) {
Pattern passwordPattern = Pattern.compile(
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[.@$!%*?&])[A-Za-z\\d@$!%*?&.]{8,}$",
Pattern.CASE_INSENSITIVE);
Matcher matcher = passwordPattern.matcher(password);
return matcher.find();
}
If I replace password
type with StringBuffer
or char[]
?
Method matcher()
in class java util.regex.Pattern
takes a single parameter whose type is CharSequence which is an interface. According to the javadoc, there are several implementing classes, including StringBuffer
. If none of the existing implementations are suitable for your needs, you can always write your own implementation.
For example, using a CharBuffer
CharBuffer cb = CharBuffer.allocate(11);
cb.put(new char[]{'S','e','c', 'r', 'e', 't', ' ', 'P', 'a', 's', 's'});
Pattern passwordPattern = Pattern.compile(
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[.@$!%*?&])[A-Za-z\\d@$!%*?&.]{8,}$",
Pattern.CASE_INSENSITIVE);
Matcher matcher = passwordPattern.matcher(cb);