217

Pythonで大文字と小文字を区別しない文字列置換を行う最も簡単な方法は何ですか?

4

10 に答える 10

255

タイプはこれ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'
于 2009-05-28T03:39:13.607 に答える
100
import re
pattern = re.compile("hello", re.IGNORECASE)
pattern.sub("bye", "hello HeLLo HELLO")
# 'bye bye bye'
于 2009-05-28T03:41:04.857 に答える
55

一行で:

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'
于 2012-03-14T20:14:03.707 に答える
21

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
于 2011-01-23T11:46:46.397 に答える
6

これは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):] 
于 2011-01-21T14:09:54.507 に答える
6

ブレアコンラッドが言うように、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'
于 2013-04-05T10:03:57.463 に答える
5

この関数はstr.replace()re.findall()関数の両方を使用します。patterninのすべての出現箇所を、大文字と小文字を区別しない方法で置き換え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
于 2019-04-16T20:17:06.260 に答える
5

構文の詳細とオプションに関する興味深い観察:

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']

于 2020-01-20T04:14:20.153 に答える
1

\ 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でテスト済み

于 2014-10-28T03:18:39.047 に答える
0
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))
于 2012-02-16T13:59:28.457 に答える