函数式编程
如果您可以从文档文件创建字符串列表,则无需使用正则表达式。只需执行这个简单的程序:fileContent = ['This is a fairy tale written by','John Doe and Mary Smith','Auckland,somewhere','This story is awesome', 'Some other things', 'story texts', 'Not Important data', 'This is a fairy tale written by','Kem Cho?','Majama?','This story is awesome', 'Not important data'] authorsList = []for i in range(len(fileContent)-3): if fileContent[i] == 'This is a fairy tale written by' and fileContent[i+3] == 'This story is awesome': authorsList.append([fileContent[i+1], fileContent[i+2]])print(authorsList)在这里,我只是检查'This is a fairy tale written by'and'This story is awesome'如果找到,则在列表中在它之间添加文本。输出:[['John Doe and Mary Smith', 'Auckland,somewhere'], ['Kem Cho?', 'Majama?']]