What is the difference in use between the Matcher() and find() methods?

Asked

Viewed 1,178 times

12

I’m trying to understand the difference in the use of these two methods of the class Matcher, but I couldn’t understand it from 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 Since been reset, at the first Character not Matched by the Previous match.

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

3 answers

12


matches() will tell if the whole string matches the regex of the Pattern;
find() will tell if you have regex in String, and if called again, if regex occurs again, and so on.

See the output of this code on 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());
  • Why did the last find() return false?

  • Because it only had twice the "http://" pattern in String. So the first two times find() finds the pattern, the third time it no longer finds.

  • It means that find() looks for the first occurrence it finds from regex inside the string and when it finds, to and returns true, and next time, it ignores occurrences it has already found?

  • 1

    In practice, that’s it. find() starts at the beginning of the string the first time it is called, and every subsequent call starts at the first character after the last pattern found (if you found any).

11

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

find() look for that pattern in string and if he finds any part that "matches" the pattern is good for him. He uses a substring. It’s like using a LIKE SQL. Or it’s like using a ^ at the beginning and a $ 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());
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • I could visualize, one has to beat the whole string, the other goes looking for "pieces" that match the regex.

4

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

Kleene star, tl;dr: repetition of the 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 house with the expression, however bbaba nonhouse.

Initially, this implies that matcher_bbab.matches() returns truth, matcher_bbaba.matches() returns false, and matcher_bbab.find() and matcher_bbaba.find() return true too. If you apply again to search with find() 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 perform twice the find() successfully, but it will not be possible to have a matches() successfully; the third find() will result in error. The same happens for bbaba.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.