pattern.matcher() vs pattern.matches()

前端 未结 8 1910
时光说笑
时光说笑 2020-11-30 06:03

I am wondering why the results of the java regex pattern.matcher() and pattern.matches() differ when provided the same regular expression and same string

         


        
8条回答
  •  爱一瞬间的悲伤
    2020-11-30 06:23

    Matcher matcher = pattern.matcher(text);
    

    In this case, a matcher object instance will be returned which performs match operations on the input text by interpreting the pattern. Then we can use,matcher.find() to match no. of patterns from the input text.

    (java.util.regex.Pattern.matches("\\+", str))
    

    Here, the matcher object will be created implicitly and a boolean will be returned which matches the whole text with the pattern. This will work as same as the str.matches(regex) function in String.

提交回复
热议问题