[英]How do I remove texts in brackets only from the beginning of lines with regex in Python?
我想刪除放在行首的括號中的所有行代碼,但想保留括號中的其他單詞。
\([^()]*\)
查找括號中的每個文本。
^\h*\([^()]*\)
只查找第一個,但不查找 rest。 我應該如何修改它?
示例文本如下:
(#p0340r#) This is a sentence. This is another one but I need more sentences to fill the space to start a new line.
(#p0350q#) Why? (this text should be left unchanged)
(#p0360r#) Because I need to remove these codes from interview texts.
預期結果應該是:
This is a sentence. This is another one but I need more sentences
to fill the space to start a new line.
Why? (this text should be left unchanged)
Because I need to remove these codes from interview texts.
謝謝!
要使用 Python re.sub
刪除任何行開頭的模式,您需要在模式之前使用^
(這是您已經擁有的)並使用flags=re.M
或 inline 傳遞re.M
標志(在-pattern) (?m)
標志。
此外, \h
不符合 Python re
兼容,您需要使用[ \t]
或[^\S\n]
之類的構造(在極少數情況下,也[^\S\r\n]
,通常當您閱讀二進制模式的文件)以匹配任何水平空格。
所以,你可以使用
re.sub(r'^[^\S\n]*\([^()]*\)[^\S\n]*', '', text, flags=re.M)
注意:如果您想在行的開頭刪除一個或多個括號內的子字符串,請將該模式分組並在其上應用+
量詞:
re.sub(r'^(?:[^\S\n]*\([^()]*\))+[^\S\n]*', '', text, flags=re.M)
# ^^^ ^^
請參閱Python 演示:
import re
text = """(#p0340r#) This is a sentence. This is another one but I need more sentences to fill the space to start a new line.
(#p0350q#) Why? (this text should be left unchanged)
(#p0360r#) Because I need to remove these codes from interview texts."""
print( re.sub(r'^[^\S\n]*\([^()]*\)[^\S\n]*', '', text, flags=re.M) )
Output:
This is a sentence. This is another one but I need more sentences to fill the space to start a new line.
Why? (this text should be left unchanged)
Because I need to remove these codes from interview texts.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.