pattern.matcher() vs pattern.matches()

前端 未结 8 1932
时光说笑
时光说笑 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:25

    From the Javadoc, see the if, and only if, the entire region section

       /**
         * Attempts to match the entire region against the pattern.
         *
         * 

    If the match succeeds then more information can be obtained via the * start, end, and group methods.

    * * @return true if, and only if, the entire region sequence * matches this matcher's pattern */ public boolean matches() { return match(from, ENDANCHOR); }

    So if your String was just "+", you'd get a true result.

提交回复
热议问题