Discuss / Python / 第二版

第二版

Topic source

雲☁

#1 Created at ... [Delete] [Delete and Lock User]

import re

def name_of_email(addr):

    pm = re.compile(r'<?(\w*\s*\w*)>?\s*?(\w*)?@\w*\.org')

    return pm.match(addr).group(1)

assert name_of_email('<Tom Paris> tom@voyager.org') == 'Tom Paris'

assert name_of_email('tom@voyager.org') == 'tom'

print('ok')

雲☁

#2 Created at ... [Delete] [Delete and Lock User]

第一版

import re

def is_valid_email(addr):

    if re.match(r'\w+?\.?\w+?@\w+?\.com',addr):

        return True

    else:

        return False

#########测试########

assert is_valid_email('someone@gmail.com')

assert is_valid_email('bill.gates@microsoft.com')

assert not is_valid_email('bob#example.com')

assert not is_valid_email('mr-bob@example.com')

print('ok')


  • 1

Reply