删除字符串中以x开头、以x结尾的字符及其之间的所有内容

2024-06-23 18:53:48 发布

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

问题:我想删除问题中的特定代码。代码会根据问题的不同而改变位置,因此我不能依赖代码的位置来删除它。在

下面是它的样子:

Now thinking specifically about the home improvement brand _Everest<.br/>On a scale of 0 to 10, where 0 is "Not at all familiar/ knowledgeable" and 10 is "Very familiar/ knowledgeable", how familiar / knowledgeable do you consider yourself to be with..."

代码-<.br>-总是附加在单词的前后。在

解决方案:我想知道,如果有一个函数,如何删除/删除一组以x开头、以x结尾的字符,并删除它们之间的所有内容。在

我希望这有道理。在


Tags: theto代码brhomeisnowabout
1条回答
网友
1楼 · 发布于 2024-06-23 18:53:48
import re

def remove_between_anchors(text, anchor):
    return re.sub(r'{0}.+?{0}'.format(anchor), '', text)

remove_between_anchors('123aa456aa789', 'aa') # returns '123789'

编辑:如果起始点/结束锚点不同:

^{pr2}$

相关问题 更多 >

    热门问题