Difference between matches() and find() in Java Regex

后端 未结 5 1888
感情败类
感情败类 2020-11-21 06:20

I am trying to understand the difference between matches() and find().

According to the Javadoc, (from what I understand), matches() will search the ent

相关标签:
5条回答
  • 2020-11-21 07:02

    matches() will only return true if the full string is matched. find() will try to find the next occurrence within the substring that matches the regex. Note the emphasis on "the next". That means, the result of calling find() multiple times might not be the same. In addition, by using find() you can call start() to return the position the substring was matched.

    final Matcher subMatcher = Pattern.compile("\\d+").matcher("skrf35kesruytfkwu4ty7sdfs");
    System.out.println("Found: " + subMatcher.matches());
    System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
    System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
    System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
    System.out.println("Found: " + subMatcher.find());
    System.out.println("Found: " + subMatcher.find());
    System.out.println("Matched: " + subMatcher.matches());
    
    System.out.println("-----------");
    final Matcher fullMatcher = Pattern.compile("^\\w+$").matcher("skrf35kesruytfkwu4ty7sdfs");
    System.out.println("Found: " + fullMatcher.find() + " - position " + fullMatcher.start());
    System.out.println("Found: " + fullMatcher.find());
    System.out.println("Found: " + fullMatcher.find());
    System.out.println("Matched: " + fullMatcher.matches());
    System.out.println("Matched: " + fullMatcher.matches());
    System.out.println("Matched: " + fullMatcher.matches());
    System.out.println("Matched: " + fullMatcher.matches());
    

    Will output:

    Found: false
    Found: true - position 4
    Found: true - position 17
    Found: true - position 20
    Found: false
    Found: false
    Matched: false
    -----------
    Found: true - position 0
    Found: false
    Found: false
    Matched: true
    Matched: true
    Matched: true
    Matched: true
    

    So, be careful when calling find() multiple times if the Matcher object was not reset, even when the regex is surrounded with ^ and $ to match the full string.

    0 讨论(0)
  • 2020-11-21 07:09

    matches return true if the whole string matches the given pattern. find tries to find a substring that matches the pattern.

    0 讨论(0)
  • 2020-11-21 07:14

    matches tries to match the expression against the entire string and implicitly add a ^ at the start and $ at the end of your pattern, meaning it will not look for a substring. Hence the output of this code:

    public static void main(String[] args) throws ParseException {
        Pattern p = Pattern.compile("\\d\\d\\d");
        Matcher m = p.matcher("a123b");
        System.out.println(m.find());
        System.out.println(m.matches());
    
        p = Pattern.compile("^\\d\\d\\d$");
        m = p.matcher("123");
        System.out.println(m.find());
        System.out.println(m.matches());
    }
    
    /* output:
    true
    false
    true
    true
    */
    

    123 is a substring of a123b so the find() method outputs true. matches() only 'sees' a123b which is not the same as 123 and thus outputs false.

    0 讨论(0)
  • matches(); does not buffer, but find() buffers. find() searches to the end of the string first, indexes the result, and return the boolean value and corresponding index.

    That is why when you have a code like

    1:Pattern.compile("[a-z]");
    
    2:Pattern.matcher("0a1b1c3d4");
    
    3:int count = 0;
    
    4:while(matcher.find()){
    
    5:count++: }
    

    At 4: The regex engine using the pattern structure will read through the whole of your code (index to index as specified by the regex[single character] to find at least one match. If such match is found, it will be indexed then the loop will execute based on the indexed result else if it didn't do ahead calculation like which matches(); does not. The while statement would never execute since the first character of the matched string is not an alphabet.

    0 讨论(0)
  • 2020-11-21 07:18

    find() will consider the sub-string against the regular expression where as matches() will consider complete expression.

    find() will returns true only if the sub-string of the expression matches the pattern.

    public static void main(String[] args) {
            Pattern p = Pattern.compile("\\d");
            String candidate = "Java123";
            Matcher m = p.matcher(candidate);
    
            if (m != null){
                System.out.println(m.find());//true
                System.out.println(m.matches());//false
            }
        }
    
    0 讨论(0)
提交回复
热议问题