对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 xml.etree.ElementTree遍历xml所有节点实例详解

python xml.etree.ElementTree遍历xml所有节点 XML文件内容: <students> <student name='刘备' s...

Python正则表达式和元字符详解

正则表达式 正则表达式是一种强大的字符串操作工具。它是一种领域特定语言 (DSL),不管是 Python 还是在大多数现代编程语言中都是作为库存在。 它们主要面向两种任务: - 验...

浅谈pycharm的xmx和xms设置方法

PyCharm使用jre,所以设置内存使用的情况和eclipse类似。 编辑PyCharm安装目录下PyCharm 4.5.3\bin下的pycharm.exe.vmoptions文件,...

啥是佩奇?使用Python自动绘画小猪佩奇的代码实例

啥是佩奇?使用Python自动绘画小猪佩奇的代码实例

最近社会猪可是火遍了大江南北,不蹭下热度可对不起它。见过手画的佩奇,见过用代码画的吗? 没有?那就来看我大显身手。 用python的turtle库来画小猪佩奇。 有人问:turtle难不...

python创建临时文件夹的方法

本文实例讲述了python创建临时文件夹的方法。分享给大家供大家参考。具体实现方法如下: import tempfile, os tempfd, tempname = tempfi...