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正則表達式匹配ip地址實例
- python利用正則表達式提取字符串
- python使用正則表達式提取網(wǎng)頁URL的方法
- python 根據(jù)正則表達式提取指定的內(nèi)容實例詳解
- 基于Python正則表達式提取搜索結(jié)果中的站點地址
- python正則實現(xiàn)提取電話功能
- Python實現(xiàn)從log日志中提取ip的方法【正則提取】
相關(guān)文章
Keras在mnist上的CNN實踐,并且自定義loss函數(shù)曲線圖操作
這篇文章主要介紹了Keras在mnist上的CNN實踐,并且自定義loss函數(shù)曲線圖操作,具有很好的參考價值,希望對大家有所幫助。2021-05-05
Conda環(huán)境導(dǎo)出與導(dǎo)入的實現(xiàn)
本文主要介紹了Conda環(huán)境導(dǎo)出與導(dǎo)入的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Django關(guān)于事務(wù)transaction.atomic()的使用方式
這篇文章主要介紹了Django關(guān)于事務(wù)transaction.atomic()的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
Python列表轉(zhuǎn)一維DataFrame的完整指南
在數(shù)據(jù)處理領(lǐng)域,Pandas的DataFrame是當(dāng)之無愧的王者,本文將用5個核心方法,教你優(yōu)雅地將一維列表轉(zhuǎn)換為Pandas DataFrame,感興趣的可以了解下2025-04-04
讓Python腳本暫停執(zhí)行的幾種方法(小結(jié))
這篇文章主要介紹了讓Python腳本暫停執(zhí)行的幾種方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
pytorch fine-tune 預(yù)訓(xùn)練的模型操作
這篇文章主要介紹了pytorch fine-tune 預(yù)訓(xùn)練的模型操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

