简体   繁体   English

如何仅从 Python 中带有正则表达式的行的开头删除括号中的文本?

[英]How do I remove texts in brackets only from the beginning of lines with regex in Python?

I would like to remove all the line codes in brackets placed at the beginning of lines but want to preserve the other words in brackets.我想删除放在行首的括号中的所有行代码,但想保留括号中的其他单词。

\([^()]*\) finds every text in brackets. \([^()]*\)查找括号中的每个文本。

^\h*\([^()]*\) finds only the first one but not the rest. ^\h*\([^()]*\)只查找第一个,但不查找 rest。 How should I modify it?我应该如何修改它?

The sample text is the following:示例文本如下:

(#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.

The expected outcome should be:预期结果应该是:

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.

Thank you!谢谢!

To remove a pattern at the start of any line with Python re.sub , you need to use the ^ before the pattern (that is what you already have) and pass the re.M flag using flags=re.M or inline (in-pattern) (?m) flag.要使用 Python re.sub删除任何行开头的模式,您需要在模式之前使用^ (这是您已经拥有的)使用flags=re.M或 inline 传递re.M标志(在-pattern) (?m)标志。

Also, \h is not Python re compliant, you need to use a construct like [ \t] or [^\S\n] (in some rare cases, also [^\S\r\n] , usually when you read a file in binary mode) to match any horizontal whitespace.此外, \h不符合 Python re兼容,您需要使用[ \t][^\S\n]之类的构造(在极少数情况下,也[^\S\r\n] ,通常当您阅读二进制模式的文件)以匹配任何水平空格。

So, you can use所以,你可以使用

re.sub(r'^[^\S\n]*\([^()]*\)[^\S\n]*', '', text, flags=re.M)

Note: if you ever want to remove one or more substrings inside parentheses at the start of a line group the pattern and apply the + quantifier on it:注意:如果您想在行的开头删除一个或多个括号内的子字符串,请将该模式分组并在其上应用+量词:

re.sub(r'^(?:[^\S\n]*\([^()]*\))+[^\S\n]*', '', text, flags=re.M)
#         ^^^                  ^^

See the Python demo :请参阅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: 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