简体   繁体   English

如何将 Pattern 和 Matcher 与 StringBuffer 或 char[] 类型而不是 String 一起使用?

[英]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[] ?如果我用StringBufferchar[]替换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.

相关问题 如何使用模式或匹配器替换字符串中的模式? - How to use pattern or matcher for replacing pattern in string? Java Pattern.matcher(StringBuffer),为什么它的行为方式与Pattern.matcher(String)不同? - Java Pattern.matcher(StringBuffer), why it does not behave the same way as Pattern.matcher(String)? Pattern Matcher Vs String Split,我应该使用哪个? - Pattern Matcher Vs String Split, which should I use? 如何在Java中使用模式和匹配器将大字符串拆分为特定的子字符串? - How do I use pattern and matcher to split a large string up into specific substrings in Java? 如何使用Matcher正确替换字符串中所有出现的模式 - How to use Matcher to properly replace all occurences of a pattern inside a string 如何在java中使用Pattern matcher? - how to use Pattern matcher in java? 为什么在使用 StringBuffer 后我不能使用 toLowerCase()? - How come I can't use toLowerCase() after using a StringBuffer? 如何在Matcher组上追加替换而不是整个模式? - How to appendReplacement on a Matcher group instead of the whole pattern? 使用模式匹配器对字符串中的单词进行计数 - Use pattern matcher to count words in a string JNI我可以使用哪些类型而不是给定(unsigned int,const char *,const wchar_t *,...) - JNI what types can I use instead of given (unsigned int, const char*, const wchar_t*, … )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM