android - why string.matches and pattern.matches get different results? -
// 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 2011/8/15 15:10-17:40,18:30-21:00"; system.out.println(cell); system.out.println("--- ---"); 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) ); } results:
2011/7/31 2011/8/15 15:10-17:40,18:30-21:00 --- --- false --- b --- 2011/7/31 2011/8/15 why string.matches return false? pattern.matches can matches?
because string#matches return true "if, , if, string matches given regular expression". can check documentation here : string#matches
however matcher#find return true " if, , if, subsequence of input sequence matches matcher's pattern". documentation available here : matcher#find.
so sum - string#matches returns true if whole string matched regex , matcher#find returns true if subsequence of string can matched regex.
Comments
Post a Comment