How to remove all text within a parenthesis?

Asked

Viewed 594 times

0

I want to remove all the text within a parenthesis, along with the parentheses, to return only the text that is outside them, as follows:

texto = 'bla bla bla (ble ble)'

I want you to turn:

texto = 'bla bla bla'

I need to change several cells into one dataframe which have this format, if possible in Python.

  • 2

    And if you have parentheses within parentheses, 'bla (ble (bli))', should remove all the same?

  • I think we can adapt this algorithm (exchanging the keys in parentheses and removing the section in question instead of saving it)

2 answers

1

You want to generate a new text that contains all the characters in the original text, except those surrounded by parentheses. In other words, you want to measure the depth of each excerpt of the original text and discard those excerpts whose depth is greater than zero. (By "depth" I mean the amount of parentheses surrounding a particular excerpt). You settle this with an accountant and a noose for.

The following is a pseudo procedural code for what I just described.

declare o contador de profundidade, inicialmente zerado
declare o novo texto, inicialmente vazio

para cada caractere C do texto original:
    se C for o caractere de abertura:
        incremente  o contador de profundidade
    senão:
        se C for o caractere de fechamento:
            decremente o contador de profundidade
        senão:
            Se o contador de profundidade estiver zerado:
                Adicione C ao novo texto

Easy to transcribe to Python syntax.

def ignorar_caracteres_cercados(texto, char_abertura, char_fechamento):
    profundidade = 0
    novo_texto = ''

    for c in texto:
        if c == char_abertura:
            profundidade += 1
        elif c == char_fechamento:
            profundidade -= 1
            if profundidade < 0:
                raise Exception('Cercamento não balanceado')
        elif profundidade == 0:
            novo_texto += c

    if profundidade > 0:
        raise Exception('Cercamento não balanceado')

    return novo_texto

Note that I have taken the liberty of adding exceptions in case the characters are unbalanced. You can remove or change this as your need.

Example of use:

texto = 'hue bla bla bla (ble ble)x(bla (bla) bla) hue (((xoxoxo))) hue ()(a)()'
print(texto)
novo_texto = ignorar_caracteres_cercados(texto, '(', ')')
print(novo_texto)

0

You can do it like this:

texto.replace(texto[texto.find("("):texto.find(")")+1], '').strip()

Who will return:

'bla bla bla'
  • Here the output of your code was 'a bla bla': https://ideone.com/YV8MUE

  • And if the parentheses are in the middle of the text it does not remove: https://ideone.com/h5Lhx3

  • @Andersoncarloswoss Corrected. Thank you for warning!

  • @Andersoncarloswoss In question it says nothing about parentheses in parentheses.

  • No, that’s why I asked the author.

  • 3

    There are many situations that are likely to happen that are not listed in the question. Parentheses within parentheses is one, another would be to have incomplete parentheses, as in '1) Olá mundo (ble)'. As described, the output should be '1) Olá mundo'. There is still the case of multiple parentheses, as in 'uma (string) (qualquer)'.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.