python匹配url中是否存在IP地址

   因为需要检测一个一个链接中是否包含了IP地址,在这里需要使用到正则表达式 ,python完美的支持了正则表达式,在这里使用re模块来完成,对正则表达式并不是很熟练,每次都是需要用的时候现查一下然后写一下,这里给出来自己的代码以及借鉴别人的匹配模式

#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
功能:对于给定的URL,检测其中是否包含IP
'''

import re


def ip_exist_two(one_url):
	compile_rule = re.compile(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])')
	match_list = re.findall(compile_rule, one_url)
	if match_list:
		print match_list
	else:
		print 'missing................'

def ip_exist_one(one_url):
	compile_rule = re.compile(r'\d+[\.]\d+[\.]\d+[\.]\d+') 
	match_list = re.findall(compile_rule, one_url)
	if match_list:
		print match_list
	else:
		print 'missing................'

if __name__ == '__main__':
	ip_list = ['http://101.23.45.67/sd/sd.html','http://www.baidu.com',
	'http://34.54.65.3/dsdfjkk.htm','http://dhj.fdjjd.com/78078979/dsdfjkk.htm']
	for one_url in ip_list:
		ip_exist_one(one_url)
	print '****************************************************'
	for one_url in ip_list:
		ip_exist_two(one_url)

ip_exist_one(one_url)里面是自己的匹配模式,个人感觉更贱练一下,ip_exist_two(one_url)里面是网上提供的匹配IP的正则表达式,感觉比较繁杂一下,不过试验了一下都是可以正确匹配出来结果的。

下面是打印出来的结果

['101.23.45.67']
missing................
['34.54.65.3']
missing................
****************************************************
['101.23.45.67']
missing................
['34.54.65.3']
missing................

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Together_CZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值