python ip正则式
时间:2022-02-13 22:13:02
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()
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python常用内置模块之xml模块(详解)
![](https://img.aspxhome.com/file/2023/1/93641_0s.jpg)
MySQL常用维护管理工具
![](https://img.aspxhome.com/file/UploadPic/20093/8/phpmyadmin-39s.jpg)
简单聊聊Golang中defer预计算参数
去掉CSS赘余代码,CSS可以更简洁
python 将列表中的字符串连接成一个长路径的方法
Python实现壁纸下载与轮换
Python tkinter的grid布局及Text动态显示方法
![](https://img.aspxhome.com/file/2023/8/87868_0s.jpg)
Python中的FTP通信模块ftplib的用法整理
SQL Server如何保证可空字段中非空值唯一
Django渲染Markdown文章目录的方法示例
![](https://img.aspxhome.com/file/2023/9/81569_0s.jpg)
一文读懂Python版本管理工具Pyenv使用
![](https://img.aspxhome.com/file/2023/0/77760_0s.png)
asp+jsp+JavaScript动态实现添加数据行
![](https://img.aspxhome.com/file/2023/9/114069_0s.png)
python去掉 unicode 字符串前面的u方法
Python 程序员必须掌握的日志记录
详解python中*号的用法
小谈MySQL字符集
ASP保存远程图片到本地 同时取得第一张图片并创建缩略图的代码
python提取内容关键词的方法
Oracle 的入门心得 强烈推荐
如何利用Matplotlib库绘制动画及保存GIF图片
![](https://img.aspxhome.com/file/2023/7/81677_0s.gif)