[Python] Python's regular expression: Non greedy optional group followed by another optional...

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

  1. Stack

    Stack Membro Participativo

    I am using the following regular expression in python: ^( .+?)?( Com:.*)?$

    (This regex might look dumb but it's actually part of a bigger more complex string, I have just extracted the problematic part.)

    And I have those 2 different strings:

    1. abc Com: 123
    2. Com: 123

    With the first string, group 1 matches only " abc" and group 2 matches " Com: 123". This is what I expect since group 1 is non greedy.

    However with the second string, I would expect group 1 to match nothing and group 2 to match again " Com: 123". But no, group 1 matches the entire string and group 2 nothing. I don't get it.

    What's going on here? Could someone explain?

    Here is the link to the regex: https://regex101.com/r/E4idh8/2

    Thanks in advance

    Continue reading...

Compartilhe esta Página