class Main { public static void main(String[] args) { //方法1,可以不加 A 和 z System.out.println(Pattern.matches("d{4}-d{2}-d{2}", "2020-06-01")); // true
//方法2,可以不加 A 和 z System.out.println("2020-06-01".matches("d{4}-d{2}-d{2}")); // true //方法3,必须加上 A 和 z Pattern pattern = Pattern.compile("Ad{4}-d{2}-d{2}z"); System.out.println(pattern.matcher("2020-06-01").find()); // true } }
class Main { public static void main(String[] args) { Pattern pattern = Pattern.compile("d{4}-d{2}"); Matcher match = pattern.matcher("2020-06 2020-07"); while (match.find()) { System.out.println(match.group()); } } }