[Python] Regular expression matches more than expected

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

  1. Stack

    Stack Membro Participativo

    Given is the following python script:

    text = '<?xml version="1.24" encoding="utf-8">'
    mu = (".??[?]?[?]", "....")
    for item in mu:
    print item,":",re.search(item, text).group()


    Can someone please explain why the first hit with the regex .??[?]?[?] returns <? instead of just ?.

    My explaination:

    • .?? should match nothing as .? can match or not any char and the second ? makes it not greedy.
    • [?]? can match ? or not, so nothing is good, too
    • [?] just matches ?

    That should result in ? and not in <?

    Continue reading...

Compartilhe esta Página