python ip正则式

yipeiwu_com5年前Python基础
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()

相关文章

Python算法之图的遍历

Python算法之图的遍历

本节主要介绍图的遍历算法BFS和DFS,以及寻找图的(强)连通分量的算法 Traversal就是遍历,主要是对图的遍历,也就是遍历图中的每个节点。对一个节点的遍历有两个阶段,首先是发现(...

Python中使用Tkinter模块创建GUI程序实例

使用Tkinter模块来创建简单的GUI程序。 Tkinter的Widgets有:Button、Canvas、Checkbutton、Entry、Frame、Label、Listbox、...

pycharm 在windows上编辑代码用linux执行配置的方法

pycharm 在windows上编辑代码用linux执行配置的方法

如下所示: 如上图所示点击右上角 ‘configure python interpreter' 弹窗如上图所示,选择项目, ‘project interpreter'  对应...

Python的Django框架中设置日期和字段可选的方法

设置字段可选 在摆弄了一会之后,你或许会发现管理工具有个限制:编辑表单需要你填写每一个字段,然而在有些情况下,你想要某些字段是可选的。 举个例子,我们想要Author模块中的email字...

利用Python的Twisted框架实现webshell密码扫描器的教程

好久以来都一直想学习windows中得iocp技术,即异步通信,但是经过长时间研究别人的c++版本,发现过于深奥了,有点吃力,不过幸好python中的twisted技术的存在方便了我。...