繁体   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