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()

标签:python,ip,正则
0
投稿

猜你喜欢

  • Django框架教程之中间件MiddleWare浅析

    2021-07-15 08:19:43
  • Python如何利用IMAP实现邮箱客户端功能

    2023-11-08 22:23:56
  • python2.7删除文件夹和删除文件代码实例

    2023-06-02 14:41:17
  • python读取word 中指定位置的表格及表格数据

    2021-04-10 02:42:21
  • python分析实现微信钉钉等软件多开分身

    2023-07-22 18:37:12
  • pytorch关于Tensor的数据类型说明

    2023-08-22 20:39:27
  • python 产生token及token验证的方法

    2023-05-19 17:25:13
  • SQL Server优化50法汇总

    2024-01-24 11:11:37
  • Python为人脸照片添加口罩实战

    2021-11-12 23:39:33
  • JavaScript画圆

    2010-01-22 15:57:00
  • python smtplib模块自动收发邮件功能(一)

    2021-04-16 07:11:18
  • 使用cmd命令行窗口操作SqlServer的方法

    2012-07-21 14:24:06
  • python绘制规则网络图形实例

    2021-08-31 02:28:28
  • Python结巴中文分词工具使用过程中遇到的问题及解决方法

    2023-04-28 18:23:28
  • linux环境下配置mysql5.6支持IPV6连接的方法

    2024-01-20 01:56:13
  • asp sql去左右空格函数

    2008-03-04 17:29:00
  • 关于Python函数参数的进阶用法

    2023-10-06 12:03:41
  • 如何让python的运行速度得到提升

    2023-04-26 21:48:35
  • js拖动div 当鼠标移动时整个div也相应的移动

    2024-04-22 22:17:20
  • pytest-fixture简介及其用法讲解

    2023-02-13 19:27:25
  • asp之家 网络编程 m.aspxhome.com