Discuss / Python / 第一题 第二题

第一题 第二题

Topic source

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


def is_valid_email(addr):
    re_match = re.compile(r'^\w+\.?\w+@\w+\.com$')
    if re.match(re_match, addr):
        return True    
    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')


def name_of_email(addr):
    re_match = re.compile(r'<?([\w\s]+)>?[\w\s]*@(\w+\.org)')
    if re.match(re_match, addr):
        return re_match.match(addr).group(1)
    return None


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

  • 1

Reply