1
我試圖從下面的行提取IPV6地址。我得到了下面的輸出。我只是想,直到IPV6地址,而不是超越that.That是我不希望看到的內容:鏈接單獨提取IPV6地址的正則表達式
代碼:
out ='ifconfig eth6.36\r\neth6.36 Link encap:Ethernet HWaddr A0:36:9F:5F:24:EE \r\n inet addr:36.36.36.10 Bcast:36.36.36.255 Mask:255.255.255.0\r\n inet6 addr: fe80::a236:9fff:fe5f:24ed/64 Scope:Link\r\n inet6 addr: 2030:30:30:30::10/64 Scope:Link\r\n UP BROADCAST MULTICAST MTU:9000 Metric:1\r\n RX packets:0 errors:0 dropped:0 overruns:0 frame:0\r\n TX packets:62 errors:0 dropped:0 overruns:0 carrier:0\r\n collisions:0 txqueuelen:0 \r\n RX bytes:0 (0.0 b) TX bytes:7004 (6.8 KiB)\r\n\r\n'
pattern = '(eth6\.36\s+Link encap:Ethernet\s*HWaddr\s*\w\w:\w\w:.+\s*inet addr:36.36.36.10\s*Bcast:36.36.36.255\s*Mask:255.255.255.0\s*\inet6 addr:\s*\w\w\w\w:.+)'
match = re.search(pattern, out)
print match.groups()
輸出:
('eth6.36 Link encap:Ethernet HWaddr A0:36:9F:5F:24:EE \r\n inet addr:36.36.36.10 Bcast:36.36.36.255 Mask:255.255.255.0\r\n inet6 addr: fe80::a236:9fff:fe5f:24ed/64 Scope:Link\r',)
。你應該只捕獲ipv6部分。 –
捕獲組是括號內的內容。移動圓括號,使它們只包含你想要捕獲的東西 –
你的意思是模式應該是pattern ='(eth6 \ .36 \ s + Link encap:Ethernet \ s * HWaddr \ s * \ w \ w:w \ w \ w:。+ \ s * inet addr:36.36.36.10 \ s * Bcast:36.36.36.255 \ s * Mask:255.255.255.0 \ s * \ inet6 addr:\ s * \ w \ w \ w \ w :)' –