[Python] Python regex capture group containing nested non-capturing group

Discussão em 'Python' iniciado por Stack, Fevereiro 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to capture string-parts Abbb, Abb, Ab, A, C###, C#, C, etc. into one group and whatever follows (anything that's not b, #) into a separate group. I'm using this regex:

    sample = "Cbb-7" # for testing purposes
    re.search(r"([A-G](?:#*|b*))(.*?)", sample).groups()


    which results in: ('C', '') while I'm expecting: ('Cbb', '-7').

    When modifying the regex to (greedy follow-up capture group(.*)):

    re.search(r"([A-G](?:#*|b*))(.*)", sample).groups()


    I get the result: ('C', 'bb-7'). (I still would need: ('Cbb','-7'))

    Continue reading...

Compartilhe esta Página