【Python面試真題】- src = "security/afafsff/?ip=123.4.56.78&id=45",請寫一段代碼用正則匹配出IP ?
【Python面試真題】- src = "security/afafsff/?ip=123.4.56.78&id=45",請寫一段代碼用正則匹配出IP ?
import re
src = "security/afafsff/?ip=123.4.56.78&id=45"
m = re.search('ip=(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})', src, re.S) # re.S 改變'.'的行為
print m.group(1)
輸出結果
123.4.56.78