我想在openvswitch.log上使用正则表达式创建模板。我有openvswitch.log,如关注文字;
2017-05-18T06:40:02.850Z|00010|ofproto_dpif|INFO|system@ovs-system: Datapath supports recirculation
2017-05-18T06:40:02.850Z|00011|ofproto_dpif|INFO|system@ovs-system: MPLS label stack length probed as 1
2017-05-18T06:40:02.850Z|00012|ofproto_dpif|INFO|system@ovs-system: Datapath supports truncate action
2017-05-18T06:40:02.850Z|00013|ofproto_dpif|INFO|system@ovs-system: Datapath supports unique flow ids
2017-05-18T06:40:02.850Z|00014|ofproto_dpif|INFO|system@ovs-system: Datapath supports ct_state
2017-05-18T06:40:02.850Z|00015|ofproto_dpif|INFO|system@ovs-system: Datapath supports ct_zone
2017-05-18T06:40:02.850Z|00016|ofproto_dpif|INFO|system@ovs-system: Datapath supports ct_mark
2017-05-18T06:40:02.850Z|00017|ofproto_dpif|INFO|system@ovs-system: Datapath supports ct_label
2017-05-18T06:40:02.850Z|00018|ofproto_dpif|INFO|system@ovs-system: Datapath supports ct_state_nat
2017-05-18T06:40:02.861Z|00001|ofproto_dpif_upcall(handler1)|INFO|received packet on unassociated datapath port 0
2017-05-18T06:40:02.861Z|00019|bridge|INFO|bridge s1: added interface s1-eth2 on port 2
2017-05-18T06:40:02.862Z|00020|bridge|INFO|bridge s1: added interface s1-eth1 on port 1
2017-05-18T06:40:02.862Z|00021|bridge|INFO|bridge s1: added interface s1-eth3 on port 3
2017-05-18T06:40:02.865Z|00022|bridge|INFO|bridge s1: added interface s1 on port 65534
2017-05-18T06:40:02.865Z|00023|bridge|INFO|bridge s2: added interface s2-eth3 on port 3
2017-05-18T06:40:02.869Z|00024|bridge|INFO|bridge s2: added interface s2-eth2 on port 2
2017-05-18T06:40:02.869Z|00025|bridge|INFO|bridge s2: added interface s2-eth1 on port 1
2017-05-18T06:40:02.873Z|00026|bridge|INFO|bridge s2: added interface s2 on port 65534
2017-05-18T06:40:02.873Z|00027|bridge|INFO|bridge s1: using datapath ID 0000000000000001
2017-05-18T06:40:02.873Z|00028|connmgr|INFO|s1: added service controller "punix:/var/run/openvswitch/s1.mgmt"
2017-05-18T06:40:02.873Z|00029|connmgr|INFO|s1: added service controller "ptcp:6654"
2017-05-18T06:40:02.873Z|00030|connmgr|INFO|s1: added primary controller "tcp:192.168.29.87:6633"
2017-05-18T06:40:02.873Z|00031|rconn|INFO|s1<->tcp:192.168.29.87:6633: connecting...
2017-05-18T06:40:02.877Z|00032|bridge|INFO|bridge s2: using datapath ID 0000000000000002
例如;
网桥 s1: 在端口 0上添加了 s1-eth2 接口
我想搜索“桥...添加界面......在端口上......” 如何使用正则表达式找到此模板?我将使用java语言
我写了一段java代码,但没找到结果
public static void main(String[] args) {
Pattern p1 = Pattern.compile("bridge (s.*?): added interface (s.*?) on port (\\d+)");
Matcher m = p1.matcher("bridge s1: added interface s1-eth2 on port 0");
String log_output = "";
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, " ");
}
m.appendTail(sb);
System.out.println(sb.toString());
}
每个人都度过愉快的一天
答案 0 :(得分:1)
你需要在哪里使用那个正则表达式?您可以使用以下命令检测行(并将各个s1 / s1-eth2 / 0结果分组)
bridge (.*?): added interface (.*?) on port (\d+)
如果您希望分隔符不同(例如制表符)或具有多个空格字符,请用\s
或\s+
替换空格。
编辑:您没有指定您使用的语言以及如何使用正则表达式。以下是如何使用Java访问组:
public static void main(String[] args) {
Pattern p1 = Pattern.compile("bridge (s.*?): added interface (s.*?) on port (\\d+)");
Matcher m = p1.matcher("bridge s1: added interface s1-eth2 on port 0");
while (m.find()) {
System.out.println("Matched: " + m.group(1) + ", " + m.group(2) + ", " + m.group(3));
// outputs: Matched: s1, s1-eth2, 0
}
}