python 字串替換 正則

2021-07-10 02:35:20 字數 501 閱讀 2551

因為看電影字幕有些不必要的想刪除,此段**用於刪除{}內的內容。

python 中 string的replace函式並不能直接使用 正規表示式,所以我們採取compile的方式:

其中re為正則的標準庫。

此段**包含:

1.檔案的讀入輸出

2.正規表示式的使用 

import re

out = open('g:/and.then.there.were.none.s01e01+.srt','w')

for line in open("g:/and.then.there.were.none.s01e01.srt"):

moduel =re.compile('')

info = re.sub(moduel,'',line)

print >> out,info

out.close()

ps 如果 .srt檔案出現亂碼,一定是編碼的錯誤, 參看如下檔案

Python 正則替換字串

需求 1.替換給定字串中符合正則匹配的子串。2.使用者配置增加 刪減替換規則方便。3.基於裝飾器模式實現。基於re包和裝飾器模式實現。參考裝飾器模式,這資料挺不錯的,有人把設計模式用python都實現了一遍。郵箱正則匹配 email regex r 0 9a za z 0 9a za z com c...

python字串替換

print replace實現多個子串替換 strr abaabbaccaaeeaa print 原字串 n format strr e strr.replace aa str 0 print 替換後的字串 n format e print split實現字串替換 strr abaabbaccaae...

python替換特定字串

python 字串替換是python操作字串的時候經常會碰到的問題,這裡簡單介紹下字串替換方法。python 字串替換可以用2種方法實現 1是用字串本身的方法。2用正則來替換字串 下面用個例子來實驗下 a hello word 我把a字串裡的word替換為python 1用字串本身的replace方...