wirdHello World, HELLO PYTHON
中的hello
替換成My
.
Da bei der Ersetzung der Funktion ?replace()“ die Gro?-/Kleinschreibung beachtet wird, wie kann Python die Zeichenfolgenersetzung ohne Berücksichtigung der Gro?-/Kleinschreibung implementieren?
小伙看你根骨奇佳,潛力無限,來學PHP伐。
參考文章:Python字符串操作相關問題
字符串不區(qū)分大小寫替換str.replace(old, new[, max])
的替換是區(qū)分大小寫的。不區(qū)分大小寫替換需要正則表達式re.sub(
)帶上re.IGNORECASE
選項。
>>> import re
>>> reg = re.compile(re.escape('hello'), re.IGNORECASE)
>>> reg.sub('My', 'Hello World, HELLO PYTHON')
'My World, My PYTHON'
import re
s = 'Hello World, HELLO PYTHON'
print re.sub(r'(?i)hello', 'My', s)