脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 对Python正则匹配IP、Url、Mail的方法详解

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

2021-05-07 00:30SnailWalking Python

今天小编就为大家分享一篇对Python正则匹配IP、Url、Mail的方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
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")      
RegularMatchEmail("109823434@qq.com")

以上这篇对Python正则匹配IP、Url、Mail的方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/zhao1999qian/article/details/56484173

延伸 · 阅读

精彩推荐