Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > Python编程技巧

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

来源:中文源码网    浏览:125 次    日期:2024-05-07 19:53:57
【下载文档:  对Python正则匹配IP、Url、Mail的方法详解.txt 】


对Python正则匹配IP、Url、Mail的方法详解
如下所示:
"""
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的方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持中文源码网。

相关内容