java-使用正則表示式擷取字串中的時間資訊
阿新 • • 發佈:2019-01-22
package com.yr.method; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @Author LYX * * @CreateTime 2015-9-7/下午2:40:25 * */ public class TimeSub { public static void main(String args[]) { Pattern pattern = Pattern.compile("[0-9]{4}[-][0-9]{1,2}[-][0-9]{1,2}[ ][0-9]{1,2}[:][0-9]{1,2}[:][0-9]{1,2}"); Matcher matcher = pattern.matcher("關於召開知識管理典型設計評審工作會議的通知 2011-4-29 15:30:18公司各部門:茲定於2011年5月4日下午14:00在"); String dateStr = null; if(matcher.find()){ dateStr = matcher.group(0); } String str =dateStr.toString(); System.out.println(str); } }
輸出為:2011-4-29 15:30:18
參考:http://www.oschina.net/code/snippet_233834_38690