文章詳情頁
string - Python字符串不區(qū)分大小寫替換
瀏覽:220日期:2022-06-27 14:18:53
問題描述
將Hello World, HELLO PYTHON中的hello替換成My。由于replace()函數(shù)替換是區(qū)分大小寫的,Python怎樣不區(qū)分大小寫實(shí)現(xiàn)字符串替換?
問題解答
回答1:參考文章:Python字符串操作相關(guān)問題
字符串不區(qū)分大小寫替換str.replace(old, new[, max])的替換是區(qū)分大小寫的。不區(qū)分大小寫替換需要正則表達(dá)式re.sub()帶上re.IGNORECASE選項(xiàng)。
>>> import re>>> reg = re.compile(re.escape(’hello’), re.IGNORECASE)>>> reg.sub(’My’, ’Hello World, HELLO PYTHON’)’My World, My PYTHON’回答2:
import res = ’Hello World, HELLO PYTHON’print re.sub(r’(?i)hello’, ’My’, s)
相關(guān)文章:
1. boot2docker無法啟動2. docker-compose中volumes的問題3. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””4. docker容器呢SSH為什么連不通呢?5. java - SSH框架中寫分頁時service層中不能注入分頁類6. dockerfile - 為什么docker容器啟動不了?7. 關(guān)于docker下的nginx壓力測試8. node.js - antdesign怎么集合react-redux對input控件進(jìn)行初始化賦值9. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.10. nignx - docker內(nèi)nginx 80端口被占用
排行榜

熱門標(biāo)簽
網(wǎng)公網(wǎng)安備