不区分大小写的替换 [英] Case insensitive replace

查看:96
本文介绍了不区分大小写的替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中执行不区分大小写的字符串替换的最简单方法是什么?

What's the easiest way to do a case-insensitive string replacement in Python?

推荐答案

字符串类型不支持此功能。您最好将正则表达式子方法 re.IGNORECASE 选项。

The string type doesn't support this. You're probably best off using the regular expression sub method with the re.IGNORECASE option.

>>> import re
>>> insensitive_hippo = re.compile(re.escape('hippo'), re.IGNORECASE)
>>> insensitive_hippo.sub('giraffe', 'I want a hIPpo for my birthday')
'I want a giraffe for my birthday'

这篇关于不区分大小写的替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆