【问题标题】:How to remove or replace substring in Python determined by start and end point?如何在 Python 中删除或替换由起点和终点确定的子字符串?
【发布时间】:2013-07-29 09:27:05
【问题描述】:

有时我会删除或替换一个长字符串的子字符串。因此,我将确定一个开始模式和一个结束模式,这将确定子字符串的起点和终点:

long_string = "lorem ipsum..white chevy..blah,blah...lot of text..beer bottle....and so to the end"
removed_substr_start = "white chevy"
removed_substr_end = "beer bott"

# this is pseudo method down
STRresult = long_string.replace( [from]removed_substr_start [to]removed_substr_end, "")

【问题讨论】:

  • 您是否考虑过使用正则表达式?
  • 很久以前,但不记得一些开始 - 结束指向的方法。
  • 我认为您过于专注于计算起点和终点,而不是您真正想要实现的目标。
  • 是的,但它似乎很有用,我想知道如果没有内置函数,我是否应该构建自己的函数。
  • 有,而且是正则表达式,看我的回答。 (编辑:现在所有其他答案)

标签: python regex string


【解决方案1】:

我猜你想要这样的东西,没有正则表达式:

def replace_between(text, begin, end, alternative=''):
    middle = text.split(begin, 1)[1].split(end, 1)[0]
    return text.replace(middle, alternative)

未测试,您应该保护第一行免受异常影响(如果未找到 begin 或 end),但想法就在这里 :)

【讨论】:

  • 嘿,伙计们....都+1。但是这个没有重新导入。非常感谢关于字符串的讲座。
  • 有很多方法可以做到这一点。我并不是说一种方法比另一种更好。但是您应该清楚自己要达到的目标,并在接受答案时保持开放的心态。此解决方案涉及当函数已存在于正则表达式中时编写额外的代码。导入模块有什么问题?这是 Python 设计的工作方式。
  • 例如为什么不写已经存在的代码,这里有个bug。尝试一个结束字符串在开始字符串之前的字符串。它将删除字符串的一部分。不是你所期望的。
  • 如果输入为"lorem ipsum..beer bottle..blah,blah...lot of text..white chevy....and so to the end",则输出为"lorem ipsum..beer bottle..blah,blah...lot of text..white chevy"。结局去哪儿了?
  • 酷。很抱歉挑了漏洞,但任何人都可以过来复制粘贴并将错误引入他们自己的代码中。你永远不知道!
【解决方案2】:

你可以使用regex:

>>> import re
>>> strs = "lorem ipsum..white chevy..blah,blah...lot of text..beer bottle....and so to the end"
>>> sub_start = "white chevy"
>>> sub_end = "beer bott"
>>> re.sub(r'{}.*?{}'.format(re.escape(sub_start),re.escape(sub_end)),'',strs)
'lorem ipsum..le....and so to the end'

如果您只想删除"white chevy""beer bott" 之间的子字符串而不是这些单词:

>>> re.sub(r'({})(.*?)({})'.format(re.escape(sub_start),
                                               re.escape(sub_end)),r'\1\3',strs)
'lorem ipsum..white chevybeer bottle....and so to the end'

【讨论】:

  • @JonClements 好主意。
【解决方案3】:

使用string.find() 获取起始索引,使用string.rfind() 获取最后一个索引,然后使用以下方法删除内部部分:

lindex = string.find(long_string, removed_substr_start)
rindex = string.find(long_string, removed_substr_end, lindex)
result = long_string[0:lindex] + longstring[rindex:]

见:http://docs.python.org/2/library/string.html#string.find

【讨论】:

  • 这也应该考虑。
  • 使用str 的方法,string 模块不再用于这些方法。还有这个won't work in py3.x
【解决方案4】:
import re
regexp = "white chevy.*?beer bott"
long_string = "lorem ipsum..white chevy..blah,blah...lot of text..beer bottle....and so to the end"
re.sub(regexp, "", long_string)

给予:

'lorem ipsum..le....and so to the end'

【讨论】:

    【解决方案5】:

    在使用了很多方法之后,我发现这个解决方案最好不用正则表达式:

    def getString( str, _from, _to ):
        end_from = str.find( _from ) +len( _from)
        return str[ end_from : str.find( _to, end_from ) ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多