JAVA regex matcher() VS find()

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,157评论 0 10
  • 如果有一天,我变得洒脱,失去了稚嫩的外表,没有了强颜的微笑,是不是就可以去外界流浪。如果有一天,我心爱的人来到我身...
    峰也阅读 1,313评论 0 4
  • 毛子裹紧了脖子上的红围巾蹲在桥头,年末的冷风像带着怨气般来回扫荡,地上枯黄的树叶被拖来扯去,发出哧哧地哀吼。 ”妈...
    柑橘与柠檬啊阅读 2,812评论 0 0
  • 每个学诗的,都是从抄袭模仿中一步步来的,我分享一些我自己验证过的东西。 01 抄袭,抄最好的 我等学渣抄作业(学霸...
    写诗兔阅读 3,499评论 0 3
  • 虽然有点污,但是这首歌和花粥的《老中医》、《流氓》相似,搞怪但上口。
    爱在忘左边阅读 3,262评论 0 0