Discuss / Python / 作业2

作业2

Topic source

Super-String

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

import re

def name_of_email(addr):
    m1 = re.match(r'^<(.*)>\s*([\w\.-]+)@([\w\.-]+)', addr)
    if m1:
        return m1.group(1)
    m2 = re.match(r'^([\w\.-]+)@([\w\.-]+)', addr)
    if m2:
        return m2.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