Pythonで大文字と小文字を区別しない文字列置換を行う最も簡単な方法は何ですか?
10 に答える
タイプはこれstring
をサポートしていません。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'
一行で:
import 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'
または、オプションの「フラグ」引数を使用します。
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の答えを続けると、この関数は1つではなく、すべての古いものを新しいものに変更します-大文字と小文字を区別しない方法で。
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
これはRegularExpを必要としません
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):]
ブレアコンラッドが言うように、string.replaceはこれをサポートしていません。
正規表現を使用しますre.sub
が、最初に置換文字列をエスケープすることを忘れないでください。2.6にはflags-optionがないことに注意してください。そのre.sub
ため、埋め込み修飾子'(?i)'
(またはREオブジェクト、Blair Conradの回答を参照)を使用する必要があります。また、別の落とし穴は、文字列が指定されている場合、subが置換テキストのバックスラッシュエスケープを処理することです。これを回避するには、代わりにラムダを渡すことができます。
関数は次のとおりです。
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()
関数の両方を使用します。pattern
inのすべての出現箇所を、大文字と小文字を区別しない方法で置き換えstring
ます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
構文の詳細とオプションに関する興味深い観察:
Python 3.7.2(tags / v3.7.2:9a3ffc0492、2018年12月23日、23:09:28)[MSC v.1916 64ビット(AMD64)] on win32
import re
old = "TREEROOT treeroot TREerOot"
re.sub(r'(?i)treeroot', 'grassroot', old)
「草の根草の根草の根」
re.sub(r'treeroot', 'grassroot', old)
「TREEROOT草の根TREerOot」
re.sub(r'treeroot', 'grassroot', old, flags=re.I)
「草の根草の根草の根」
re.sub(r'treeroot', 'grassroot', old, re.I)
「TREEROOT草の根TREerOot」
したがって、一致式に(?i)プレフィックスを付けるか、4番目の引数として「flags = re.I」を追加すると、大文字と小文字が区別されない一致が発生します。ただし、4番目の引数として「re.I」だけを使用しても、大文字と小文字を区別しない一致は発生しません。
比較のために、
re.findall(r'treeroot', old, re.I)
['TREEROOT'、'treeroot'、'TREerOot']
re.findall(r'treeroot', old)
['treeroot']
\ tをエスケープシーケンスに変換していたので(少し下にスクロール)、re.subがバックスラッシュされたエスケープ文字をエスケープシーケンスに変換することに注意しました。
それを防ぐために、私は次のように書きました。
大文字と小文字を区別せずに置き換えます。
import re
def ireplace(findtxt, replacetxt, data):
return replacetxt.join( re.compile(findtxt, flags=re.I).split(data) )
また、特別な意味のバッシュスラッシュ文字をエスケープシーケンスに変換する他の回答のように、エスケープ文字に置き換えたい場合は、検索結果をデコードするか、文字列を置き換えます。Python 3では、.decode( "unicode_escape")#python3のようなことをしなければならないかもしれません。
findtxt = findtxt.decode('string_escape') # python2
replacetxt = replacetxt.decode('string_escape') # python2
data = ireplace(findtxt, replacetxt, data)
Python2.7.8でテスト済み
i='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))