对Python正则匹配IP、Url、Mail的方法详解

yipeiwu_com5年前Python基础

如下所示:

"""
Created on Thu Nov 10 14:07:36 2016


@author: qianzhewoniuqusanbu
"""
import re
def RegularMatchIP(ip):
    '''进行正则匹配ip,加re.IGNORECASE是让结果返回bool型'''
    pattern=re.match(r'\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$',ip,re.IGNORECASE)
    if pattern:
        print ip
    else:
        print "The IP address format is incorrect!"
        

def RegularMatchUrl(url):
    pattern=re.match(r'(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?',url,re.IGNORECASE)
    if pattern:
        print url
    else:
        print "invalid url"
        
        
def RegularMatchEmail(email):
     pattern=re.match(r'\w+@([0-9a-zA-Z]+[-0-9a-zA-Z]*)(\.[0-9a-zA-Z]+[-0-9a-zA-Z]*)+',email,re.IGNORECASE)
     if pattern:
         print email
     else:
         print "invalid eamil"


RegularMatchIP("12.32.35.23")      
RegularMatchUrl("http://c.biancheng.net/cpp/html/1435.html")
RegularMatchEmail("109823434@qq.com")

以上这篇对Python正则匹配IP、Url、Mail的方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python中对list去重的多种方法

今天遇到一个问题,在同事随意的提示下,用了 itertools.groupby 这个函数。不过这个东西最终还是没用上。 问题就是对一个list中的新闻id进行去重,去重之后要保证顺序不变...

Python Learning 列表的更多操作及示例代码

遍历列表-for循环 列表中存储的元素可能非常多,如果想一个一个的访问列表中的元素,可能是一件十分头疼的事。那有没有什么好的办法呢?当然有!使用 for循环 假如有一个食物名单列表,通过...

python skimage 连通性区域检测方法

涉及到的函数为 import matplotlib.pyplot as plt from skimage import measure, color labels = measure...

python numpy实现文件存取的示例代码

python numpy实现文件存取的示例代码

NumPy提供了多种存取数组内容的文件操作函数。保存数组数据的文件可以是二进制格式或者文本格式。二进制格式的文件又分为NumPy专用的格式化二进制类型和无格式类型。 一,tofile()...

Python代码缩进和测试模块示例详解

前言 Python代码缩进和测试模块是大家学习python必不可少的一部分,本文主要介绍了关于Python代码缩进和测试模块的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看...