簡體   English   中英

如何僅從 Python 中帶有正則表達式的行的開頭刪除括號中的文本?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM