如何使用包含域的电子邮件验证域

时间:2012-06-27 05:57:16

标签: python regex

您好我正在寻找一种方法来验证使用提供的电子邮件地址的域名 域名可以是  http://www.test.free.com  http://www.free.com.au  http://www.free.com.au.org  http://www.free.com.org  或者可以存在具有任意数量的子子域和任何数量的验证的域名。

验证邮件应该包含域名,这是我到目前为止所写的内容

if domain_url is not None and verification_email is not None:

    logging.info('==============================')
    logging.info('BOTH WERE NOT NONE')
    logging.info('==============================')

    domain_email = verification_email.split('@')[1].split('.')[0]

    logging.info('==============================')
    logging.info(domain_email)
    logging.info('==============================')

    verify_domain_url_list = domain_url.split('.')

    if len(verify_domain_url_list) == 4:
        verify_domain_url_list.pop()
        verify_domain_url_list.pop()
    if len(verify_domain_url_list) == 3:
        verify_domain_url_list.pop()


    verify_status = domain_email in verify_domain_url_list

    if verify_status:
        return 'yes'
    else:
        pre_domain_address = verify_domain_url_list[0].split('//')[1]
        if pre_domain_address is not None and \
            domain_email in pre_domain_address:
            return 'yes'
        else:
            return 'no'
    return 'no' 

但这并不适合所有可能性。任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您可以这样做,只需在括号内添加新域。

verification_email = 'fed@test.free.com'
pattern = r'.*@(test.free.com|free.com.au|free.com.au.org|free.com.org)$'

import re
if re.search( pattern , verification_email):
    print('yes')
    # or on your case, return 'yes'

https://github.com/fedmich/StackOverflow-answers/blob/master/python-11220230/regex.py