What is the difference between the match () and find () methods of the Matcher class?

13

I'm trying to understand the difference in use of these two class methods Matcher , but I could not quite understand the description of the documentation that says:

  

public boolean matches ()
  Attempts to match the entire region against the pattern.

     

public boolean find ()
  Attempts to find the next subsequence of the input sequence that matches the pattern.
  This method starts at the beginning of this matcher's region, or, if a previous invocation of the method was successful and the matcher has not been reset, at the first character matched by the previous match.

What is the difference in use between these two methods? If possible, I would like to see the difference exemplified.

    
asked by anonymous 02.10.2017 / 14:32

3 answers

13

matches() will tell if the integer String hits the regex of the pattern;
find() will say if it has the regex in the String, and if called again, if the regex occurs again, and so on.

See the output of this code in Ideone

String text =
            "This is the text to be searched " +
                "for occurrences of the http:// pattern. Find http:// again";

    String patternString = ".*http://.*";    
    Pattern pattern = Pattern.compile(patternString);    
    Matcher matcher = pattern.matcher(text);
    System.out.println(matcher.matches());

    String patternString2 = "http://";    
    Pattern pattern2 = Pattern.compile(patternString2);    
    Matcher matcher2 = pattern2.matcher(text);
    System.out.println(matcher2.matches());
    System.out.println(matcher2.find());
    System.out.println(matcher2.find());
    System.out.println(matcher2.find());
    
02.10.2017 / 14:52
11

matches() search for all the string , ie the pattern you are looking for should be exactly the same as the string being used to search. It's like you're using an equal.

find() search for that pattern in string and if you find any part that "hits" the pattern is good for it. It uses a substring . It's like using LIKE of SQL. Or it's like using ^ at the beginning and $ at the end in a regular expression.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Program {
    public static void main(String[] args) throws Exception {
        Matcher fonte = (Pattern.compile("test")).matcher("teste de regex");
        System.out.println(fonte.find());
        System.out.println(fonte.matches());
    }
}

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

    
02.10.2017 / 14:43
4

Let's get the expression to match (bb|([^b].|b[^b]))* . This expression can be understood as "two bê or not two bê" applied to the Kleene star .

  

Kleene star , tl; dr: repeated element quantified by * , may appear zero or more times

By definition, an even number of letters is required to match this expression. This means that bbab matches expression, but bbaba does not match.

Initially, this implies that matcher_bbab.matches() returns true, matcher_bbaba.matches() returns false, and matcher_bbab.find() and matcher_bbaba.find() return true as well. If you apply again to search with find() it will fail in both cases.

Now, let's take the same expression only without the Kleene star: (bb|([^b].|b[^b])) . In the case of bbab , it will be possible to double the find() successfully, but it will not be possible to have a matches() successfully; the third% w / w% will result in an error. The same happens for find() .

    
02.10.2017 / 14:54