在Python中,最简单的进行不区分大小写字符串替换的方法是什么?
string
类型不支持此操作。最好使用带有正则表达式sub方法和re.IGNORECASE选项。
>>> 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'
import re
pattern = re.compile("hello", re.IGNORECASE)
pattern.sub("bye", "hello HeLLo HELLO")
# 'bye bye bye'
re.sub('hello', 'bye', 'hello HeLLo HELLO', flags=re.IGNORECASE)
使用正则表达式将字符串中的所有'hello'替换为'bye',忽略大小写。 - Louis Yangre.sub
仅自 Python 2.7 起支持此标志。 - fuenfundachtzigimport re
re.sub("(?i)hello","bye", "hello HeLLo HELLO") #'bye bye bye'
re.sub("(?i)he\.llo","bye", "he.llo He.LLo HE.LLO") #'bye bye bye'
或者,使用可选的 "flags" 参数:
import re
re.sub("hello", "bye", "hello HeLLo HELLO", flags=re.I) #'bye bye bye'
re.sub("he\.llo", "bye", "he.llo He.LLo HE.LLO", flags=re.I) #'bye bye bye'
在bFloch的答案基础上,这个函数将会以不区分大小写的方式替换所有出现的旧字符串为新字符串。
def ireplace(old, new, text):
idx = 0
while idx < len(text):
index_l = text.lower().find(old.lower(), idx)
if index_l == -1:
return text
text = text[:index_l] + new + text[index_l + len(old):]
idx = index_l + len(new)
return text
正如Blair Conrad所说,string.replace不支持此操作。
可以使用正则表达式re.sub
,但请记得先转义替换字符串。注意,在2.6版本的re.sub
中没有flags选项,因此您需要使用嵌入式修饰符'(?i)'
(或RE对象,请参见Blair Conrad的答案)。另外,另一个陷阱是如果传入的是字符串,sub将处理替换文本中的反斜杠转义字符。一种避免这个问题的方法是使用lambda函数。
以下是一个函数示例:
import re
def ireplace(old, repl, text):
return re.sub('(?i)'+re.escape(old), lambda m: repl, text)
>>> ireplace('hippo?', 'giraffe!?', 'You want a hiPPO?')
'You want a giraffe!?'
>>> ireplace(r'[binfolder]', r'C:\Temp\bin', r'[BinFolder]\test.exe')
'C:\\Temp\\bin\\test.exe'
此函数同时使用了 str.replace()
和 re.findall()
函数。它会以不区分大小写的方式,将 string
中所有出现的 pattern
替换为 repl
。
def replace_all(pattern, repl, string) -> str:
occurences = re.findall(pattern, string, re.IGNORECASE)
for occurence in occurences:
string = string.replace(occurence, repl)
return string
这不需要使用正则表达式
def ireplace(old, new, text):
"""
Replace case insensitive
Raises ValueError if string not found
"""
index_l = text.lower().index(old.lower())
return text[:index_l] + new + text[index_l + len(old):]
有关语法细节和选项的有趣观察:
# Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
>>> import re
>>> old = "TREEROOT treeroot TREerOot"
>>> re.sub(r'(?i)treeroot', 'grassroot', old)
'grassroot grassroot grassroot'
>>> re.sub(r'treeroot', 'grassroot', old)
'TREEROOT grassroot TREerOot'
>>> re.sub(r'treeroot', 'grassroot', old, flags=re.I)
'grassroot grassroot grassroot'
>>> re.sub(r'treeroot', 'grassroot', old, re.I)
'TREEROOT grassroot TREerOot'
(?i)
前缀或在第四个参数中添加flags=re.I
将导致不区分大小写的匹配,但仅使用re.I
作为第四个参数不会导致不区分大小写的匹配。>>> re.findall(r'treeroot', old, re.I)
['TREEROOT', 'treeroot', 'TREerOot']
>>> re.findall(r'treeroot', old)
['treeroot']
re.sub(pattern, repl, string, count=0, flags=0)
,这就是为什么flags=re.I
可以工作,但尝试将其作为位置参数传递会失败,因为它的位置不正确。 - DavidPi='I want a hIPpo for my birthday'
key='hippo'
swp='giraffe'
o=(i.lower().split(key))
c=0
p=0
for w in o:
o[c]=i[p:p+len(w)]
p=p+len(key+w)
c+=1
print(swp.join(o))
我遇到了\t被转换成转义序列的问题(请向下滚动一点),所以我注意到re.sub会将反斜杠转义字符转换为转义序列。
为了防止这种情况,我写了以下内容:
不区分大小写地替换。
import re
def ireplace(findtxt, replacetxt, data):
return replacetxt.join( re.compile(findtxt, flags=re.I).split(data) )
findtxt = findtxt.decode('string_escape') # python2
replacetxt = replacetxt.decode('string_escape') # python2
data = ireplace(findtxt, replacetxt, data)
在Python 2.7.8中测试通过
'hippo'
不是必需的,但会很有用,因此这只是一个好的例子而已。 - Blair Conradre.escape
转义你的搜索词之外,还有一个陷阱,这个答案没有避免,在https://dev59.com/5XNA5IYBdhLWcg3wjOhS#15831118中已经注意到:由于`re.sub`处理转义序列,如 https://docs.python.org/library/re.html#re.sub 中所示,你需要在替换字符串中要么转义所有反斜杠,要么使用 lambda 函数。 - Mark Ameryr'xxxA\BCxxxA\BCxxx'
中用r'A\BC'
替换为r'D\EF'
是无效的 - 正确答案是来自johv的那个。 - stenci