Discuss / Python / 第一题and第二题

第一题and第二题

Topic source

咕咕111111

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

def is_valid_email(addr): m = re.match(r'[0-9a-zA-Z.]+@[0-9a-zA-Z]+.com', addr) try: return m except AssertionError as e: print('error', e)

测试:

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(email): m = re.match(r'([<]?)([A-Za-z0-9\s]+)[>]?([\sA-Za-z0-9]+)@[a-zA-Z0-9]+.org', email) if m.group(1) == '<': return m.group(2) else: return re.match(r'([A-Za-z0-9]+)@[A-Za-z0-9]+.org', email).group(1)

测试:

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


  • 1

Reply