weixin_40404115
一支向阳花
2021-05-12 17:19
采纳率: 71.4%
浏览 32

python 多个字符不区分大小写如何进行替换

c = 'obs.oBs_222.xml  obs_123'

如何将c里面的obs.oBs和obs替换为OBPS.ODS_CBUS,最后输出OBPS.ODS_CBUS_222.xml OBPS.ODS_CBUS_123

也就是如何将字符串中的多个字符不区分大小写进行替换?

 

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • isKelel
    iMingzhen 2021-05-12 17:28
    已采纳

    还是用正则

    示例代码

    # coding=utf8
    # the above tag defines encoding for this document and is for Python 2.x compatibility
    
    import re
    
    regex = r"obs.obs|obs"
    
    test_str = "obs.oBs_222.xml  obs_123"
    
    subst = "OBPS.ODS_CBUS"
    
    # You can manually specify the number of replacements by changing the 4th argument
    result = re.sub(regex, subst, test_str, 0, re.IGNORECASE)
    
    if result:
        print (result)
    
    # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
    
    点赞 评论
  • weixin_42871902
    唯羽 2021-05-12 17:29
    c = 'obs.oBs_222.xml  obs_123'
    c = c.lower()
    c = c.replace('obs', 'OBPS.ODS_CBUS')
    print(c)
    
    点赞 评论

相关推荐