美文网首页Java
JAVA regex matcher() VS find()

JAVA regex matcher() VS find()

作者: JaedenKil | 来源:发表于2018-11-22 16:34 被阅读0次
import java.util.regex.Pattern;

public class RegexDemo {
    public static void main(String[] args) {
        String str = "Hello World.";

        System.out.println(str.matches("llo W"));
        // false
        System.out.println(str.matches(".*llo W.*"));
        // true
        System.out.println(Pattern.compile("llo W").matcher(str).matches());
        //false
        System.out.println(Pattern.compile(".*llo W.*").matcher(str).matches());
        //true
        System.out.println(Pattern.compile("llo W").matcher(str).find());
        // true
    }
}

The java.time.Matcher.matches() method attempts to match the entire region against the pattern.
Return Value
true if, and only if, the entire region sequence matches this matcher's pattern.
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

The java.time.Matcher.find(int start) method resets this matcher and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index.
Return Value
True if, and only if, a subsequence of the input sequence starting at the given index matches this matcher's pattern

Simply speaking, matches() is more similar to equals() while find() is more similar to contains().

BUT BE AWARE:

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

public class RegexDemo02 {
    public static void main(String[] args) throws InterruptedException {
        String str = "aa111bb22222cc3333dd";
        Matcher matcher = Pattern.compile("\\d+").matcher(str);
        System.out.println(matcher.find());
        System.out.println(matcher.start());
        Thread.sleep(100);

        System.out.println(matcher.find());
        System.out.println(matcher.start());
        Thread.sleep(100);

        System.out.println(matcher.find());
        System.out.println(matcher.start());
        Thread.sleep(100);

        System.out.println(matcher.find());
        System.out.println(matcher.start());
    }
}
true
2
true
7
true
14
Exception in thread "main" false
java.lang.IllegalStateException: No match available
    at java.util.regex.Matcher.start(Matcher.java:343)
    at RegexDemo02.main(RegexDemo02.java:21)

find() will try to find the next occurrence within the substring that matches the regex.
That means, the result of calling find() multiple times might not be the same.

Matcher.reset();

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

public class RegexDemo02 {
    public static void main(String[] args) throws InterruptedException {
        String str = "aa111bb22222cc3333dd";
        Matcher matcher = Pattern.compile("\\d+").matcher(str);
        System.out.println(matcher.find());
        System.out.println(matcher.start());
        Thread.sleep(100);

        matcher.reset();
        System.out.println(matcher.find());
        System.out.println(matcher.start());
        Thread.sleep(100);

        matcher.reset();
        System.out.println(matcher.find());
        System.out.println(matcher.start());
        Thread.sleep(100);

        matcher.reset();
        System.out.println(matcher.find());
        System.out.println(matcher.start());
    }
}
true
2
true
2
true
2
true
2

相关文章

网友评论

    本文标题:JAVA regex matcher() VS find()

    本文链接:https://www.haomeiwen.com/subject/cmglqqtx.html