1.txt里的内容
A girl come in, the name is Jack, evel 955;
A old lady come in, the name is Mary,level 94454;
A pretty boy come in, the name is Patrick, level 194;
# 第一步 从txt中获取名字
file_path='1.txt'
with open(file_path,'r') as f:
# 按行读 返回列表
list1=f.read().splitlines()
# 第二步 对列表中的每一个元素做拆分
# 找到name的值
def get_name(list1):
for item in list1:
name = item.split(',')[1].split(' ')[-1:]
print(name)
get_name(list1)
import re
text = "A girl come in, the name is Jack, evel 955;
A old lady come in, the name is Mary,level 94454;
A pretty boy come in, the name is Patrick, level 194;"
names = re.findall(r'the name is (\w+)', text)
print(names)