英文:
How can I use Pattern and Matcher with StringBuffer or char[] types instead of String?
问题
如何使用类似以下方法:
```java
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();
}
如果我将 password
类型替换为 StringBuffer
或者 char[]
呢?
<details>
<summary>英文:</summary>
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[]`?
</details>
# 答案1
**得分**: 3
Method `matcher()` in class `java.util.regex.Pattern` takes a single parameter whose type is [CharSequence][1] 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][2]
```java
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);
英文:
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);
答案2
得分: 1
如果你将密码类型替换为 StringBuffer
:
按原样使用,即以下代码将能够成功编译并按预期工作:
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();
}
如果你将密码类型替换为 char[]
:
使用 passwordPattern.matcher(String.valueOf(password));
。
英文:
> if I replace password type with StringBuffer or char[]?
If you replace password type with StringBuffer
:
Use as it is i.e. 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[]
:
Use passwordPattern.matcher(String.valueOf(password));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论