Python 判断字符串是否为IP(字符串中是否包含IP)
生活随笔
收集整理的這篇文章主要介紹了
Python 判断字符串是否为IP(字符串中是否包含IP)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 檢查字符串是否為IP
import redef isIP(str):p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')if p.match(str):return Trueelse:return FalsemyStr = "255.255.abc.255"if isIP(myStr):print(myStr,"is a IP!") else:print(myStr, "is not a IP!")運行結果為:
255.255.abc.255 is not a IP!
?
2. 判斷字符串中是否包含IP
def ip_exist_two(one_url):compile_rule = re.compile(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])')match_list = re.findall(compile_rule, one_url)if match_list:print (match_list)else:print('missing................')def ip_exist_one(one_url):compile_rule = re.compile(r'\d+[\.]\d+[\.]\d+[\.]\d+')match_list = re.findall(compile_rule, one_url)if match_list:print( match_list)else:print ('missing................')運行結果為:
['192.168.8.24']
總結
以上是生活随笔為你收集整理的Python 判断字符串是否为IP(字符串中是否包含IP)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DNS常用记录类型和服务发现(DNS解析
- 下一篇: python 中的 del 使用方法