我有一个字符串,它有许多子字符串,我想删除。你知道吗
每个子字符串以ApPle
开始,以THE BEST PIE — STRAWBERRY
结束。你知道吗
我尝试了关于this post的建议,但是没有用。你知道吗
输入
Cannoli (Italian pronunciation: [kanˈnɔːli]; Sicilian: cannula) are Italian ApPle Sep 12 THE BEST PIE —
STRAWBERRY pastries that originated on the island of Sicily and are today a staple of Sicilian cuisine1[2] as well as Italian-American cuisine. Cannoli consist of tube-shaped shells of fried pastry dough, filled with a sweet, creamy filling usually ApPle Aug 4 THE BEST PIE — STRAWBERRY containing ricotta. They range in size from "cannulicchi", no bigger than a finger, to the fist-sized proportions typically found south of Palermo, Sicily, in Piana degli Albanesi.[2]
import re
array = []
#open the file and delete new lines
with open('canoli.txt', 'r') as myfile:
file = myfile.readlines()
array = [s.rstrip('\n') for s in file]
text = ' '.join(array)
attempt1 = re.sub(r'/ApPle+THE.BEST.PIE.-.STRAWBERRY/','',text)
attempt2 = re.sub(r'/ApPle:.*?:THE.BEST.PIE.-.STRAWBERRY/','',text)
print(attempt1)
print(attempt2)
期望输出
Cannoli (Italian pronunciation: [kanˈnɔːli]; Sicilian: cannula) are Italian pastries that originated on the island of Sicily and are today a staple of Sicilian cuisine1[2] as well as Italian-American cuisine. Cannoli consist of tube-shaped shells of fried pastry dough, filled with a sweet, creamy filling usually containing ricotta. They range in size from "cannulicchi", no bigger than a finger, to the fist-sized proportions typically found south of Palermo, Sicily, in Piana degli Albanesi.[2]
我想你的正则表达式应该是:
ApPle.*?THE\sBEST\sPIE\s—\sSTRAWBERRY
您需要添加regex选项DOTALL以正确处理换行符,请尝试以下操作:
相关问题 更多 >
编程相关推荐