[英]How can I use Pattern and Matcher with StringBuffer or char[] types instead of String?
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[]
?如果我用
StringBuffer
或char[]
替换password
类型?
Method matcher()
in class java util.regex.Pattern
takes a single parameter whose type is CharSequence which is an interface.类
java util.regex.Pattern
方法matcher()
接受一个类型为CharSequence的单个参数,它是一个接口。 According to the javadoc , there are several implementing classes, including StringBuffer
.根据javadoc ,有几个实现类,包括
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
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);
if I replace password type with StringBuffer or char[]?
如果我用 StringBuffer 或 char[] 替换密码类型?
If you replace password type with StringBuffer
:如果用
StringBuffer
替换密码类型:
Use as it is ie the following will compile successfully and work as intended:按原样使用,即以下内容将成功编译并按预期工作:
private boolean respectPattern(StringBuffer 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 you replace password type with char[]
:如果用
char[]
替换密码类型:
Use passwordPattern.matcher(String.valueOf(password));
使用
passwordPattern.matcher(String.valueOf(password));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.