[Python] How to match numbers that don't contain decimal point using python regex only

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

  1. Stack

    Stack Membro Participativo

    I have an input string "10 3.14 5 2.718" and want to get the output ["10", "5"] using regex only.

    I tried Gemini and Copilot but both couldn't produce the right output as their proposed solutions keep using \b in the regex pattern, no matter how many times I prompt their solutions don't work. Problem is decimal point also works as a word boundary (\b).

    My code as below:

    import re
    text = "10 3.14 5 2.718"
    pattern = r"\d+(?!\.\d+)"
    matches = re.findall(pattern, text)
    print(matches)

    Continue reading...

Compartilhe esta Página