不知道,我正確地知道什麼是MAC地址,但請細下面兩個選項:
輸入:
text = """
Adapter slot="3" enabled="false" Mcable="true" speed="0" type="Am79C973">
Adapter slot="3" enabled="false" MACAddress="080027671C79" cable="true" speed="0" type="Am79C973">
Adapter slot="3" enabled="false" MACAddress="080027671C71" cable="true" speed="2" type="Am79C973">
Adapter slot="3" enabled="false" MACAddress="080024671C79" cable="true" speed="1" type="Am79C973">
Adapter slot="3" enabled="false" MACAddress="080227671C79" cable="true" speed="1" type="Am79C973">
"""
1)正則表達式:
from re import findall
findall(r'(?i)(?<=MACAddress=\")\w{12}', text)
2)字符串切片:
searchTxt = 'MACAddress="'
searchLen = len(searchTxt)
getMAC = lambda line: line[line.index(searchTxt) + searchLen: line.index(searchTxt) + searchLen + 12]
map(lambda line: getMAC(line), (line for line in text.split() if searchTxt in line))
@Artsiom Rudzenka請問**(?i)**部分是什麼? – eyquem 2011-06-13 08:15:49
@eyquem - 忽略大小寫 - 例如案例無心無力 – 2011-06-13 08:21:23
@Artsiom Rudzenka謝謝。你確定這是Python中可用的功能嗎?在我看來,它存在於其他語言中,但不在Python中。沒看過。請給我一個參考嗎?在Python中,忽略大小寫:** re.compile(pattern,re.IGNORECASE)** – eyquem 2011-06-13 10:39:23