Discuss / Python / 第二题

第二题

Topic source

Json_Zhang

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

第二题

def name_of_email(addr):
    re_s = re.match(r'(<([a-zA-Z0-9\s\_]*)>)?\s*(.+)\@(.+\..+)$',addr)

    if re_s.group(2) != '' and re_s.group(2) != None:
            return re_s.group(2)
    elif re_s.group(3) != '' and re_s.group(3) != None:
            return re_s.group(3)
    else:
            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