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

yipeiwu_com6年前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 FTP两个文件夹间的同步实例代码

具体代码如下所示: # -*- coding: utf-8 -*- ''''''' ftp自动检测源文件夹的更新,将源文件夹更新的内容拷贝到目标文件夹中 使用树的层序遍...

python安装Scrapy图文教程

python安装Scrapy图文教程

安装方法 pip install Scrapy 如果顺利的话不用管直接一路下来就OK 验证是否安装成功 安装成功 不顺利的情况 1)lxml安装不成功 使用whl进行安装,不过需要先...

python twilio模块实现发送手机短信功能

python twilio模块实现发送手机短信功能

前排提示:这个模块不是用于对陌生人进行短信轰炸和电话骚扰的,这个模块也没有这个功能,如果是抱着这个心态来的,可以关闭网页了 语言:python 步骤一:安装twilio模块 pip in...

使用graphics.py实现2048小游戏

使用graphics.py实现2048小游戏

1、过年的时候在手机上下载了2048玩了几天,心血来潮决定用py写一个,刚开始的时候想用QT实现,发现依赖有点大。正好看到graphics.py是基于tkinter做的封装就拿来练手,并...

Python函数的返回值、匿名函数lambda、filter函数、map函数、reduce函数用法实例分析

Python函数的返回值、匿名函数lambda、filter函数、map函数、reduce函数用法实例分析

本文实例讲述了Python函数的返回值、匿名函数lambda、filter函数、map函数、reduce函数用法。分享给大家供大家参考,具体如下: 函数的返回值: 函数一旦执行到&...