在Python中查找字符串而不拆分?

时间:2015-12-31 09:56:40

标签: python

我的练习目的是获取IP,子网掩码。我写了我的练习,但我的老师说"不要使用split()来剪切和找到字符串。您的代码重复"。

这是我的代码:

regex_ip = "(\d{1,3}.){3}\d{1,3}"
dic = {}  
ip = ""
mac = ""
subnet = ""
status = ""
type = ""
list_main = content.split('\n\n')
for i in range(len(list_main)):
    if list_main[i].find("Ethernet") == 0 or list_main[i].find("Wireless")    == 0 :  
    type = list_main[i]
    y = i+1
    list = list_main[y].split("\n")
        for x in range(len(list)):
           if "IPv4" in list[x] or "inet addr" in list[x]:
             strs = re.search(r'{}'.format(regex_ip),list[x])
             ip = strs.group()
           if "Physical" in list[x]:
               strs = re.search(r'([a-zA-Z0-9]{2}-){5}[a-zA-Z0-9]                 {2}',list[x] )
               mac = strs.group()
           if "Subnet" in list[x]:
               strs = re.search(r'{}'.format(regex_ip),list[x])
               subnet = strs.group()
        di = {'ip':ip, 'netmask':subnet, 'mac':mac}
        dic[type] = di 
print dic

获取IP的内容,子网:

"""Windows IP Configuration

   Host Name . . . . . . . . . . . . : Windows8_64
   Primary Dns Suffix  . . . . . . . :
   Node Type . . . . . . . . . . . . : Hybrid
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No

Wireless LAN adapter Local Area Connection* 11:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual Adapter
   Physical Address. . . . . . . . . : 1E-55-F9-17-14-BE
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes

Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Qualcomm Atheros AR5B97 Wireless Network Adapter
   Physical Address. . . . . . . . . : EC-55-F9-17-14-BE
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::846:7c08:a6b7:c239%16(Preferred)
   IPv4 Address. . . . . . . . . . . : 192.168.1.106(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Lease Obtained. . . . . . . . . . : Wednesday, December 30, 2015 7:15:16 PM
   Lease Expires . . . . . . . . . . : Thursday, December 31, 2015 7:15:16 PM
   Default Gateway . . . . . . . . . : 192.168.1.100
   DHCP Server . . . . . . . . . . . : 192.168.1.100
   DHCPv6 IAID . . . . . . . . . . . : 384587257
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-19-BC-5B-79-00-1E-90-81-D1-14
   DNS Servers . . . . . . . . . . . : 203.113.188.1
                                   203.113.131.3
   NetBIOS over Tcpip. . . . . . . . : Enabled

Ethernet adapter Ethernet 2:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Broadcom NetLink (TM) Gigabit Ethernet
   Physical Address. . . . . . . . . : 1C-75-08-DC-5C-5C
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes

Ethernet adapter VMware Network Adapter VMnet1:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet1
   Physical Address. . . . . . . . . : 00-50-56-C0-00-01
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::346e:efd2:8d5:b17f%23(Preferred)
   IPv4 Address. . . . . . . . . . . : 192.168.174.1(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :
   DHCPv6 IAID . . . . . . . . . . . : 352342102
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-19-BC-5B-79-00-1E-90-81-D1-14
   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                   fec0:0:0:ffff::2%1
                                   fec0:0:0:ffff::3%1
   NetBIOS over Tcpip. . . . . . . . : Enabled

Ethernet adapter VMware Network Adapter VMnet8:

    Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet8
   Physical Address. . . . . . . . . : 00-50-56-C0-00-08
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . :   fe80::7ddf:f88d:ff65:b79e%24(Preferred)
   IPv4 Address. . . . . . . . . . . : 192.168.19.1(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :
   DHCPv6 IAID . . . . . . . . . . . : 369119318
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-19-BC-5B-79-00-1E-90-81-D1-14
   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                   fec0:0:0:ffff::2%1
                                   fec0:0:0:ffff::3%1
   NetBIOS over Tcpip. . . . . . . . : Enabled

2 个答案:

答案 0 :(得分:-1)

您可以使用 str.find(string_to_search,begin,end)方法。

str是您要搜索的字符串 string_to_search是要搜索的字符串 begin是搜索开始的索引。 结束索引,默认搜索结束是str的长度。

返回第一个发生模式的索引。

答案 1 :(得分:-2)

ipaddr =" 10.10.10.1 255.255.255.0" 进口重新 IPADDR ' 10.10.10.1 255.255.255.0' x = re.match(r"([0-9] +。[0-9] +。[0-9] +。[0-9] +)([0-9] +。[0- 9] + [0-9] + [0-9] +)",IPADDR,如re.I | re.M被) x.group() ' 10.10.10.1 255.255.255.0' x.group(0) ' 10.10.10.1 255.255.255.0'

  
    
      

x.group(1)       ' 10.10.10.1'       x.group(2)       ' 255.255.255.0'