2012-10-14 522 views
0

假設我有以下的輸出:的Tcl:匹配字符串僅當後跟一個數字[正則表達式]

Loopback1 is up, line protocol is up 
    Hardware is Loopback 
    Description: ** NA4-ISIS-MGMT-LOOPBACK1_MPLS ** 
    Internet address is 84.116.226.27/32 
    MTU 1514 bytes, BW 8000000 Kbit, DLY 5000 usec, 
    reliability 255/255, txload 1/255, rxload 1/255 
    Encapsulation LOOPBACK, loopback not set 
    Keepalive set (10 sec) 
    Last input 12w3d, output never, output hang never 
    Last clearing of "show interface" counters never 
    Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0 
    Queueing strategy: fifo 
    Output queue: 0/0 (size/max) 
    5 minute input rate 0 bits/sec, 0 packets/sec 
    5 minute output rate 0 bits/sec, 0 packets/sec 
    0 packets input, 0 bytes, 0 no buffer 
    Received 0 broadcasts (0 IP multicasts) 
    0 runts, 0 giants, 0 throttles 
    0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort 
    6 packets output, 456 bytes, 0 underruns 
    0 output errors, 0 collisions, 0 interface resets 
    0 output buffer failures, 0 output buffers swapped out 

我如何能匹配「Loopback1接口」,而不是「回」? 換句話說,只有在Tcl旁邊有一個數字時,我如何才能匹配接口名稱?

回答

3

使用前瞻

Loopback(?=\d+) 

它僅匹配環路回送後跟任意數量的數字。如果你想匹配環回和數字,使用Loopback\d+

+0

謝謝Pogo :)我感興趣的只是找到模式,不匹配的數字,所以第一應該是好的,你介意簡要地解釋什麼「? =「呢? – user690182

+0

?=預測特定模式。在迴環匹配之後的In/Loopback(?= \ d +)/之後,它展望未來找到\ d +。如果找到\ d +,則Loopback是匹配的,否則不匹配。 – pogo

+0

Pogo,你知道LookAhead是否比Tcl.v8更早版本? – user690182