例:
import re
test= '$MYNETACT: 0,1,"10.10.0.9"'
pattern =re.compile(r'"(\d+\.\d+\.\d+\.\d+)"') # 正則表達式,匹配IP地址
print(pattern.findall(test)) # 獲取ip地址
運行結果:
['10.10.0.9']
findall
在字符串中找到正則表達式所匹配的所有子串,並返回一個列表,如果沒有找到匹配的,則返回空列表。
注意: match 和 search 是匹配一次 findall 匹配所有。
語法格式為:
re.findall(pattern, string, flags=0) 或 pattern.findall(string[, pos[, endpos]])
參數:
- pattern 匹配模式。
- string 待匹配的字符串。
- pos 可選參數,指定字符串的起始位置,默認為 0。
- endpos 可選參數,指定字符串的結束位置,默認為字符串的長度。
本次用到我的腳本里面的應用,主要是為了獲取到一個socket鏈接的ID和ip地址: