python ip正則式
更新時間:2009年05月07日 00:45:35 作者:
python下的ip正則實現(xiàn)代碼。
ip正則式為:r'(([12][0-9][0-9]|[1-9][0-9]|[1-9])\.){3,3}([12][0-9][0-9]|[1-9][0-9]|[1-9])'
以下為一個示例
#-*- coding:utf-8 -*-
import re
def ip():
'驗證IP的正則式'
def match_group(p):
s = '''211.210.209.108
gan ffad1.210.2.108
d ffad1.210.2.109afa'''
com = re.compile(p)
lst_m = com.finditer(s)
for m in lst_m:
print m.group()
p = r'(([12][0-9][0-9]|[1-9][0-9]|[1-9])\.){3,3}([12][0-9][0-9]|[1-9][0-9]|[1-9])'
match_group(p)
def group():
'''若存在多個匹配,可以用finditer來獲取到多個組'''
def match(p):
s = 'Isaac Newton, physicist, huang zhijun'
mo = re.compile(p)
m = mo.search(s)
if not m:
print 'no match'
else:
print mo.findall(s)
print 'm.group(0):', m.group(0)
# print 'm.group(1):', m.group(1)
# print 'm.group(2):', m.group(2)
m_ite = mo.finditer(s)
for ite in m_ite:
print 'ite.group(0)', ite.group(0)
print 'ite.group(1)', ite.group(1)
print 'ite.group(2)', ite.group(2)
# p = r'(\w+) (\w+)'
p = r'(\w+) (\w+)'
match(p)
if __name__ == '__main__':
ip()
# group()
以下為一個示例
#-*- coding:utf-8 -*-
import re
def ip():
'驗證IP的正則式'
def match_group(p):
s = '''211.210.209.108
gan ffad1.210.2.108
d ffad1.210.2.109afa'''
com = re.compile(p)
lst_m = com.finditer(s)
for m in lst_m:
print m.group()
p = r'(([12][0-9][0-9]|[1-9][0-9]|[1-9])\.){3,3}([12][0-9][0-9]|[1-9][0-9]|[1-9])'
match_group(p)
def group():
'''若存在多個匹配,可以用finditer來獲取到多個組'''
def match(p):
s = 'Isaac Newton, physicist, huang zhijun'
mo = re.compile(p)
m = mo.search(s)
if not m:
print 'no match'
else:
print mo.findall(s)
print 'm.group(0):', m.group(0)
# print 'm.group(1):', m.group(1)
# print 'm.group(2):', m.group(2)
m_ite = mo.finditer(s)
for ite in m_ite:
print 'ite.group(0)', ite.group(0)
print 'ite.group(1)', ite.group(1)
print 'ite.group(2)', ite.group(2)
# p = r'(\w+) (\w+)'
p = r'(\w+) (\w+)'
match(p)
if __name__ == '__main__':
ip()
# group()
您可能感興趣的文章:
- python標(biāo)準(zhǔn)日志模塊logging的使用方法
- Python中使用logging模塊打印log日志詳解
- python改變?nèi)罩?logging)存放位置的示例
- 使用python分析git log日志示例
- Python正則表達(dá)式匹配ip地址實例
- python利用正則表達(dá)式提取字符串
- python使用正則表達(dá)式提取網(wǎng)頁URL的方法
- python 根據(jù)正則表達(dá)式提取指定的內(nèi)容實例詳解
- 基于Python正則表達(dá)式提取搜索結(jié)果中的站點(diǎn)地址
- python正則實現(xiàn)提取電話功能
- Python實現(xiàn)從log日志中提取ip的方法【正則提取】
相關(guān)文章
詳解使用Pytorch Geometric實現(xiàn)GraphSAGE模型
這篇文章主要為大家介紹了詳解使用Pytorch Geometric實現(xiàn)GraphSAGE模型示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04OpenCV3.3+Python3.6實現(xiàn)圖片高斯模糊
這篇文章主要為大家詳細(xì)介紹了OpenCV3.3+Python3.6實現(xiàn)圖片高斯模糊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05使用pipenv管理python虛擬環(huán)境的全過程
pipenv 是Kenneth Reitz大神的作品,能夠有效管理Python多個環(huán)境,各種包,接下來通過本文給大家分享使用pipenv管理python虛擬環(huán)境的全過程,感興趣的朋友一起看看吧2021-09-09Python機(jī)器學(xué)習(xí)之KNN近鄰算法
KNN可以說是最簡單的分類算法之一,同時,它也是最常用的分類算法,文中非常詳細(xì)的介紹了該算法,對正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05Pytorch訓(xùn)練模型得到輸出后計算F1-Score 和AUC的操作
這篇文章主要介紹了Pytorch訓(xùn)練模型得到輸出后計算F1-Score 和AUC的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05Python爬蟲數(shù)據(jù)的分類及json數(shù)據(jù)使用小結(jié)
這篇文章主要介紹了Python爬蟲數(shù)據(jù)的分類及json數(shù)據(jù)使用小結(jié),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03