问题描述
把这两条日志用一个正则表达式进行匹配!Oct 24 23:18:05 10.46.10.66 Oct 24 2006 23:04:53: %ASA-3-713119: Group = operator, Username = lif, IP = 222.76.76.240, PHASE 1 COMPLETED Oct 24 22:47:28 10.46.10.65 Oct 24 2006 22:20:15: %ASA-3-713902: Group = fmcc-vpn, IP = 58.23.98.254, Removing peer from peer table failed, no match! 问题补充:chen_yongkai 写道
解决方案
public static void main(String[] args) {String[] logs = {"Oct 24 23:18:05 10.46.10.66 Oct 24 2006 23:04:53: %ASA-3-713119: Group = operator, Username = lif, IP = 222.76.76.240, PHASE 1 COMPLETED","Oct 24 22:47:28 10.46.10.65 Oct 24 2006 22:20:15: %ASA-3-713902: Group = fmcc-vpn, IP = 58.23.98.254, Removing peer from peer table failed, no match!" };Pattern p = Pattern.compile("\%ASA-3-([0-9]{6}):\sGroup\s=\s([^,]*),(?:\sUsername\s=\s([^,]*),)*\sIP\s=\s([^,]*),");for (String log : logs) {Matcher m = p.matcher(log);if (m.find()) {int n = m.groupCount();for (int i = 1; i <= n; i++) {String find = m.group(i);if (find != null)System.out.println(find);}}System.out.println("=========");}}
解决方案二:
public static void main(String[] args) {String log1 = "Oct 24 23:18:05 10.46.10.66 Oct 24 2006 23:04:53: %ASA-3-713119: Group = operator, Username = lif, IP = 222.76.76.240, PHASE 1 COMPLETED";String log2 = "Oct 24 22:47:28 10.46.10.65 Oct 24 2006 22:20:15: %ASA-3-713902: Group = fmcc-vpn, IP = 58.23.98.254, Removing peer from peer table failed, no match!";Pattern p = Pattern.compile("\%ASA-3-[0-9]{6}:\sGroup\s=\s");System.out.println(p.matcher(log1).find());System.out.println(p.matcher(log2).find());}你应该说明匹配的特征是什么?