个人信息提取_正则表达式

"""
# 正则表达式
import re

str = 'abc123cd45'
ret1 = re.search('\d+', str).group()
ret2 = re.findall('\d+', str)
print(ret1)  # 输出:123
print(ret2)  # 输出:['123', '45']



# filter()函数
# (python2 中)
filter(str.isdigit, '123ab45')
# 结果是'12345'
# (python3 中)
"".join(list(filter(str.isdigit, '123ab45')))
# 结果是'12345'
"""

# 题目解答
import re

information = input()
ls1 = information.split(' ')
ls2 = re.findall('\d+', information)
born_year = 0
for item in ls2:
    if len(item) == 4:
        born_year = item
print(f'姓名:{ls1[1]}\n班级:{ls1[2]}\n出生:{born_year}年')

你可能感兴趣的:(python)