一支向阳花 2021-05-12 17:19 采纳率: 52.6%
浏览 49
已采纳

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条回答 默认 最新

  • 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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退