[Python] regular expression for numeric range in python

Discussão em 'Python' iniciado por Stack, Maio 12, 2025.

  1. Stack

    Stack Membro Participativo

    I need to find numeric range in the format "number-number". the number should be in the range 0-3000. so I came up with this regular expression

    match = re.search(r'^[1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]-[1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]',sentence)


    When I ran the program I wanted to extract only 56-900 in the sentence but the program extracted other numbers also like 2016, CLP2012 etc.. I wanted to extract only numbers that have "-" in between them. What is wrong in my pattern.

    Continue reading...

Compartilhe esta Página