2017-05-21 125 views
1

我想用正則表達式在openvswitch.log上創建一個模板。我有openvswitch.log跟隨文本;用正則表達式創建模板

2017-05-18T06:40:02.850Z|00010|ofproto_dpif|INFO|[email protected]: Datapath supports recirculation 
2017-05-18T06:40:02.850Z|00011|ofproto_dpif|INFO|[email protected]: MPLS label stack length probed as 1 
2017-05-18T06:40:02.850Z|00012|ofproto_dpif|INFO|[email protected]: Datapath supports truncate action 
2017-05-18T06:40:02.850Z|00013|ofproto_dpif|INFO|[email protected]: Datapath supports unique flow ids 
2017-05-18T06:40:02.850Z|00014|ofproto_dpif|INFO|[email protected]: Datapath supports ct_state 
2017-05-18T06:40:02.850Z|00015|ofproto_dpif|INFO|[email protected]: Datapath supports ct_zone 
2017-05-18T06:40:02.850Z|00016|ofproto_dpif|INFO|[email protected]: Datapath supports ct_mark 
2017-05-18T06:40:02.850Z|00017|ofproto_dpif|INFO|[email protected]: Datapath supports ct_label 
2017-05-18T06:40:02.850Z|00018|ofproto_dpif|INFO|[email protected]: 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:添加接口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()); 

    } 

有一個愉快的一天大家

回答

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 
    } 
} 
+0

它沒有找到一個結果:( – can

+0

你沒有指定你想用正則表達式做什麼,也不是你用什麼語言檢查更新。答案。 – zwer

+0

我更新了問題謝謝你的提醒:) – can