[Python] How to extract text outside parentheses using regex?

Discussão em 'Python' iniciado por Stack, Julho 23, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to extract the part of a string that is outside parentheses using regular expressions.

    For example, from the input string:

    "Hello (this is extra) world (more text)"



    I want to extract:

    "Hello world"



    I’ve tried patterns like [^()]+, but they either remove too much or match what's inside the parentheses.

    Can someone explain a correct regex pattern (with explanation) that captures only the text outside the parentheses, including cases with multiple sets of parentheses?

    I'm working in Python, but a general explanation also helps.

    Continue reading...

Compartilhe esta Página