// pattern 20xx/xx/xx, e.g. 2012/2/22
String Regex_Date_1 = "20\\d\\d/\\d{1,2}/\\d{1,2}";
String cell = "from 2011/7/31 to 2011/8/15 15:10-17:40,18:30-21:00";
System.out.println(cell);
System.out.println("--- a ---");
System.out.println( cell.matches(Regex_Date_1) );
System.out.println("--- b ---");
Pattern p = Pattern.compile(Regex_Date_1);
Matcher m = p.matcher(cell);
while(m.find()){
System.out.println( m.group(0) );
}
结果:
from 2011/7/31 to 2011/8/15 15:10-17:40,18:30-21:00
--- a ---
false
--- b ---
2011/7/31
2011/8/15
为什么 string.matches 返回 false?但是 pattern.matches 可以得到匹配?