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

c = 'obs.oBs_222.xml  obs_123'

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

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

 

还是用正则

示例代码

# 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.

 

c = 'obs.oBs_222.xml  obs_123'
c = c.lower()
c = c.replace('obs', 'OBPS.ODS_CBUS')
print(c)