【问题标题】:How to subtract multiple string values from another string [duplicate]如何从另一个字符串中减去多个字符串值[重复]
【发布时间】:2016-09-15 13:29:38
【问题描述】:

与:

abc = 'abc'
xyz = 'xyz'

word = 'begin abc- middle_xyz_ end'

我需要从word 中提取abcxyz 的值。 结果应该是

result = 'begin - middle__ end'

如何用最少的代码做到这一点?

【问题讨论】:

  • re.sub 来自 re 模块。

标签: python


【解决方案1】:

您使用带有空字符串的replace() 作为要替换的值。

result = word.replace('abc','').replace('xyz','')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    • 2019-07-27
    相关资源
    最近更新 更多