Python正则匹配IP、Url、Mail的方法与代码示例

2018-12-2520:40:47后端程序开发Comments2,609 views字数 949阅读模式
分享一篇对Python正则匹配IP、Url、Mail的方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随菜鸟学院小编过来看看吧。

如下所示:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9062.html

"""
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")
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9062.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/9062.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定