Python:在Python中查找并删除以特定子字符串开头和结尾的字符串

2024-04-29 00:35:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字符串,它有许多子字符串,我想删除。你知道吗

每个子字符串以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]


Tags: ofthe字符串inappleasarebest
1条回答
网友
1楼 · 发布于 2024-04-29 00:35:32

我想你的正则表达式应该是:ApPle.*?THE\sBEST\sPIE\s—\sSTRAWBERRY

您需要添加regex选项DOTALL以正确处理换行符,请尝试以下操作:

re.sub(r'ApPle.*?THE\sBEST\sPIE\s—\sSTRAWBERRY','',text, flags=re.DOTALL)

相关问题 更多 >