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()
相關(guān)文章
詳解使用Pytorch Geometric實現(xiàn)GraphSAGE模型
這篇文章主要為大家介紹了詳解使用Pytorch Geometric實現(xiàn)GraphSAGE模型示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
OpenCV3.3+Python3.6實現(xiàn)圖片高斯模糊
這篇文章主要為大家詳細介紹了OpenCV3.3+Python3.6實現(xiàn)圖片高斯模糊,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05
使用pipenv管理python虛擬環(huán)境的全過程
pipenv 是Kenneth Reitz大神的作品,能夠有效管理Python多個環(huán)境,各種包,接下來通過本文給大家分享使用pipenv管理python虛擬環(huán)境的全過程,感興趣的朋友一起看看吧2021-09-09
Pytorch訓(xùn)練模型得到輸出后計算F1-Score 和AUC的操作
這篇文章主要介紹了Pytorch訓(xùn)練模型得到輸出后計算F1-Score 和AUC的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
Python爬蟲數(shù)據(jù)的分類及json數(shù)據(jù)使用小結(jié)
這篇文章主要介紹了Python爬蟲數(shù)據(jù)的分類及json數(shù)據(jù)使用小結(jié),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03

