[Python] Problem while retrieving entire word match from regex with findall()

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

  1. Stack

    Stack Membro Participativo

    I'm trying to get the entire word that matches the expression. I can do it for one match, with search() but there are several in the string.

    With this code I get exactly what I want, but only the first one.

    re_pattern = r"(\d+(\.\d+){2,})-(20\d{2})(\d{2})(\d{2})-XPTO"
    s = "sdfljkfslk fdjf dslkjfkfj \n | ksldfjflkj \n 1.3.6-20241129-XPTO slkj lkj | ## lkjlkjd ssss jkjkj$$=1.3.6-20241129-XPTO"

    matches = re.search(re_pattern, s)
    print(matches[0])


    Prints 1.3.6-20241129-XPTO

    When trying to use findall() I get very different results:

    matches = re.findall(re_pattern, s)
    print(matches)


    Produces [('1.3.6', '.6', '2024', '11', '29'), ('1.3.6', '.6', '2024', '11', '29')]

    Can't figure out why. Any help is appreciated, thanks.

    Continue reading...

Compartilhe esta Página