62,614
社区成员
发帖
与我相关
我的任务
分享
Pattern p = Pattern.compile("([\\d-: ]+)\\[.+?\\]-\\[");
String log = "2015-07-11 15:16:47 [ pool-2-thread-2:74158 ]-[ INFO ]";
Matcher m = p.matcher(log);
while(m.find()){
System.out.println(m.group(1).trim());
}
import java.util.regex.*;
public class Test {
public static String extractDateTime(String text) {
Pattern pattern = Pattern.compile("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
return matcher.group(0);
}
return null;
}
public static void main(String[] args) {
System.out.println(extractDateTime("2015-07-11 15:16:47 [ pool-2-thread-2:74158 ]-[ INFO ] "));
System.out.println(extractDateTime("bbbbbbb"));
}
}
输出
2015-07-11 15:16:47
null