pattern.matcher() vs pattern.matches()

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

    matches tries to match the expression against the entire string. Meaning, it checks whether the entire string is a patern or not. conceptually think it like this, it implicitly adds a ^ at the start and $ at the end of your pattern.

    For, String str = "hello+", if you want matches() to return true, you need to have pattern like ".\+."

    I hope this answered your question.

提交回复
热议问题