对Python正则匹配IP、Url、Mail的方法详解
程序员文章站
2022-05-02 11:58:13
如下所示:
"""
created on thu nov 10 14:07:36 2016
@author: qianzhewoniuqusanbu...
如下所示:
""" 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的方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
上一篇: 数据库常用的sql语句汇总