我已經通過了關於該主題的每篇文章,並且沒有任何回覆我的問題;在Python上從字符串中提取整數值
這是代碼:
output = 'Scan results for BrainS (192.168.43.111) Scan results for Slave (192.168.43.107) Scan results for SlaveSmall (192.168.43.242)' while (True) if output[i].isdigit(): # i has initial value of 15, j=0 f[j]=output[i] j+=1 i+=1 elsif(i == len(output)): break else: i+=1 continue
打印:
>>>f ['1','9','2','1','6','8','4','3','1','1','1','0','0','0']
正如你可以看到我試圖提取IP,因爲它是用點(在此代碼我沒有嘗試提取點,但只有數字), 我無法弄清楚如何得到我想要完全的字符串,因爲它是: F = 192.168.43.111
有什麼建議嗎?更好的命令?
正則表達式是答案:http://stackoverflow.com/questions/10086572/ip-address-validation-in-python-using-regex – zemekeneng