pattern.matcher() vs pattern.matches()

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

    pattern.matcher(String s) returns a Matcher that can find patterns in the String s. pattern.matches(String str) tests, if the entire String (str) matches the pattern.

    In brief (just to remember the difference):

    • pattern.matcher - test if the string contains-a pattern
    • pattern.matches - test if the string is-a pattern
    0 讨论(0)
  • 2020-11-30 06:04

    The code equivalent to java.util.regex.Pattern.matches("\\+", str) would be:

    Pattern.compile("\\+").matcher(str).matches();
    

    method find will find the first occurrence of the pattern in the string.

    0 讨论(0)
  • 2020-11-30 06:13

    Matcher.find() attempts to find the next subsequence of the input sequence that matches the pattern.

    Pattern.matches(String regex, CharSequence input) compiles the regex into a Matcher and returns Matcher.matches().

    Matcher.matches attempts to match the entire region (string) against the pattern (Regex).

    So, in your case, the Pattern.matches("\\+", str) returns a false since str.equals("+") is false.

    0 讨论(0)
  • 2020-11-30 06:13

    I think your question should really be "When should I use the Pattern.matches() method?", and the answer is "Never." Were you expecting it to return an array of the matched substrings, like .NET's Matches methods do? That's a perfectly reasonable expectation, but no, Java has nothing like that.

    If you just want to do a quick-and-dirty match, adorn the regex with .* at either end, and use the string's own matches() method:

    System.out.println(str.matches(".*\\+.*"));
    

    If you want to extract multiple matches, or access information about a match afterward, create a Matcher instance and use its methods, like you did in your question. Pattern.matches() is nothing but a wasted opportunity.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题