Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
Extract text with multiple separators

I have list of strings with separators A and B:

L = ['sgfgfqds A aaa','sderas B ffff','eeee','sdsdfd A rrr']

and need:

L1 = [['aaa'], ['ffff'], ['eeee'], ['rrr']] 

I tried using:

L1 = [re.findall(r'(?<=A)(.*)$', inputtext) for inputtext in L]
print (L1)

but, it returns the following:

[[' aaa'], [], [], [' rrr']] 

How can I get the desired output?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can use re.split to break your strings on either 'A' or 'B':

>>> L1 = [re.split(r'[AB] *', inputtext)[-1] for inputtext in L]
>>> L1
['aaa', 'ffff', 'eeee', 'rrr']
over 4 years ago · Santiago Trujillo Relatório

0

Alternative suggestion without regex.

[[i] for i in ' '.join(L).split(' ') if i.count(i[0]) == len(i) and len(i) > 1]

Result

 [['aaa'], ['ffff'], ['eeee'], ['rrr']]
over 4 years ago · Santiago Trujillo Relatório

0

You can use the fact that split returns a list even if it doesn't find the separator.

 L1 = [[x.split(' A ')[-1].split(' B ')[-1]] for x in L]
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda