1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Regular expressions for extracting text inside curly brackets [closed]

Discussão em 'Python' iniciado por Stack, Novembro 29, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working with some marked text and I need to extract information in order to use later. I want to use regular expressions from Python using the module re, but I can't construct the right expression. I have two situations:


    • Text in format string="{some text}{other text 1}{other text 2}". Here I use the regexp "\\{(.*?)\\}" but I obtain

      >> string="{some text}{other text 1}{other text 2}"
      >> elements = re.split("\\{(.*?)\\}",string)
      >> print(elements)
      >> ['', 'some text', '', 'other text 1', '', 'other text 2', '']


      I can't understand why the empty strings appear in positions 0, 2, 4 and 6. If I edit my original string to string="}{some text}{other text 1}{other text 2}{" and use the regexp "\\}\\{(.*?)\\}\\{" I obtain

      >> string="}{some text}{other text 1}{other text 2}{"
      >> elements = re.split("\\}\\{(.*?)\\}\\{",string)
      >> print(elements)
      >> ['', 'some text', 'other text 1', 'other text 2', '']


      the internal empty strings in the output dissapear, but not the first and last. How should I construct the regular expression in order to obtain only the elements inside brackets?


    • Text in format string="some text {other text}". In this case I need to extract "some text" and also "other text". Here I don't know how to proceed.

    Can someone help me, please?

    Continue reading...

Compartilhe esta Página