我有這種字串:
hello[_ng11][test]hello3[_ngRTf]
我想洗掉以_ng括號內開頭的字串
結果應該是:
hello[][test]hello3[]
我確實嘗試過這樣做:
st = "hello[_ng11][test]hello3[_ngRTf]"
modified_string = re.sub(r"/\[\[_ng[^\]]*\]\]/", "[]", st)
print(modified_string)
uj5u.com熱心網友回復:
您需要洗掉斜線并將方括號減少到模式兩側的單個出現:
modified_string = re.sub(r"\[_ng[^][]*]", "[]", st)
請參閱Python 演示:
import re
st = "hello[_ng11][test]hello3[_ngRTf]"
modified_string = re.sub(r"\[_ng[^][]*]", "[]", st)
print(modified_string)
# => hello[][test]hello3[]
詳情:
\[_ng- 一個[_ng字串([這里只有 char 是特殊的)[^][]*]- 除了and之外的零個或多個字符[(智能放置,]是字符類中的第一個字符,因此不需要轉義)]- 一個]字符(它在字符類之外并不特殊)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/520765.html
標籤:Python正则表达式
上一篇:進一步縮小正則運算式字符集,以便強制使用一個大寫字母
下一篇:正則運算式獲取不帶括號的數字()
