简体   繁体   中英

How to remove a string from brackets including the brackets with a condition?

I have a question similar to this one .

I want to remove brackets and the text within, but keep some words. For example I have a list:

["Going", "(#)", "(maybe)", "to", "the", "(##)", "(mall)", "market"]

I want to keep (#) and (##) , but remove (maybe) and (mall) .

Expected output:

["Going", "(#)", "to", "the", "(##)", "market"]

There can be any number of words with brackets in the list just like 'maybe' and 'mall' .

Brackets with # can have maximum of 3 hash.

You can use a list-comprehension to filter the original list using a regular expression:

import re

a = ["Going", "(#)", "(maybe)", "to", "the", "(##)", "(mall)", "market"]
b = [word for word in a if re.match(r"[^(]|\(#{1,3}\)", word)]

Gives:

['Going', '(#)', 'to', 'the', '(##)', 'market']

re.match matches the pattern from the beginning of the string. The pattern means:

  • [^(] - any character except ( .
  • | - or...
    • \( - literal parenthesis
    • #{1,3} - 1 to 3 repetitions of #
    • \) - literal parenthesis

Regex demo

In a generic way you can parse the list and assess if the word has a pair of brackets. If it does, and if the word inside is not #, ## or ### , then you should exclude it from the output. Assuming you have a list of strings:

a = ['Going', '(#)', '(maybe)', 'to', 'the', '(##)', '(mall)', 'market']

output = [word for word in a if ('(' not in word and ')' not in word) or word.strip('()') in ['#', '##', '###']]
print(output)
# ['Going', '(#)', 'to', 'the', '(##)', 'market']

The strip method keeps only the string within the given parameters (in this case ( and ) ).

You can learn more about list comprehensions here:https://www.w3schools.com/python/python_lists_comprehension.asp

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