文章内容

2020/1/14 14:23:05,作 者: 黄兵

Python通过正则表达式检测是否电子邮件地址

下面是Python通过正则表达式匹配来验证是否是电子邮件地址:

# Python program to validate an Email 
  
# import re module 
  
# re module provides support 
# for regular expressions 
import re 
  
# Make a regular expression 
# for validating an Email 
regex = '^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$'
      
# Define a function for 
# for validating an Email 
def check(email):  
  
    # pass the regualar expression 
    # and the string in search() method 
    if(re.search(regex,email)):  
        print("Valid Email")  
          
    else:  
        print("Invalid Email")  
      
  
# Driver Code  
if __name__ == '__main__' :  
      
    # Enter the email  
    email = "[email protected]"
      
    # calling run function  
    check(email) 
  
    email = "[email protected]"
    check(email) 
  
    email = "ankitrai326.com"
    check(email) 

re.search(),如果模式不匹配,返回None。


文章来源:Check if email address valid or not in Python

分享到:

发表评论

评论列表