如何在 StringBuffer 或 char[] 类型中使用 Pattern 和 Matcher,而不是 String?

huangapple go评论72阅读模式
英文:

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)(?=.*[.@$!%*?&amp;])[A-Za-z\\d@$!%*?&amp;.]{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[]{&#39;S&#39;,&#39;e&#39;,&#39;c&#39;, &#39;r&#39;, &#39;e&#39;, &#39;t&#39;, &#39; &#39;, &#39;P&#39;, &#39;a&#39;, &#39;s&#39;, &#39;s&#39;});
Pattern passwordPattern = Pattern.compile(
                &quot;^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[.@$!%*?&amp;])[A-Za-z\\d@$!%*?&amp;.]{8,}$&quot;,
                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(
            &quot;^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[.@$!%*?&amp;])[A-Za-z\\d@$!%*?&amp;.]{8,}$&quot;,
            Pattern.CASE_INSENSITIVE);
    Matcher matcher = passwordPattern.matcher(password);
    return matcher.find();
}

If you replace password type with char[]:

Use passwordPattern.matcher(String.valueOf(password));

huangapple
  • 本文由 发表于 2020年10月12日 00:44:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64306576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定