简体   繁体   中英

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

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.

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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM