對於學校我應該編寫一個提取IP地址的Python RE腳本。我正在使用的正則表達式似乎與re.search()
一起使用,但不與re.findall()
一起使用。Python正則表達式 - re.search()vs re.findall()
exp = "(\d{1,3}\.){3}\d{1,3}"
ip = "blah blah 192.168.0.185 blah blah"
match = re.search(exp, ip)
print match.group()
的匹配,始終是192.168.0.185,但它的不同,當我做re.findall()
exp = "(\d{1,3}\.){3}\d{1,3}"
ip = "blah blah 192.168.0.185 blah blah"
matches = re.findall(exp, ip)
print matches[0]
0.
我不知道爲什麼re.findall()
收益率0時re.search()
產生192.168.0.185,因爲我m對這兩個函數使用相同的表達式。
我該怎麼做才能讓re.findall()
真的能正確地遵循這個表達式?或者我犯了一些錯誤?
這很好,謝謝。 – user1168906