[問題] re.sub的用法

看板Python作者 (future)時間3年前 (2020/07/05 11:32), 3年前編輯推噓4(402)
留言6則, 3人參與, 3年前最新討論串1/1
抱歉菜雞想問個 import re str = "NOC??????????????NSF" a = re.sub("?","a",str) print(a) 這個print出來的錯誤我看不太懂 有人能解釋一下嗎?? 謝謝~~ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.117.195.230 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1593919920.A.B48.html

07/05 11:41, 3年前 , 1F
?在regex是保留字元 要變成\?跳過
07/05 11:41, 1F

07/05 12:39, 3年前 , 2F
前面改成\? 假如是要用a取代str裡面的問號
07/05 12:39, 2F
原來如此~ 學習到了,謝謝!! ※ 編輯: andrew5106 (122.117.195.230 臺灣), 07/05/2020 13:43:01

07/05 14:52, 3年前 , 3F

07/05 18:32, 3年前 , 4F
學Regex的時候,pattern通常會用raw string的形式呈現
07/05 18:32, 4F

07/05 18:32, 3年前 , 5F
,也會比較簡單,raw string就是字串前面加r,像這樣r
07/05 18:32, 5F

07/05 18:32, 3年前 , 6F
"我是菜鳥”。
07/05 18:32, 6F
文章代碼(AID): #1V0Kcmj8 (Python)