[Python] Regex exact match

Discussão em 'Python' iniciado por Stack, Janeiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following sentence: "The size of the lunch box is around 1.5l or 1500ml"

    How can I change this to: "The size of the lunch box is around 1.5 liter or 1500 milliliter"

    In some cases, the value might also be present as "1.5 l or 1500 ml" with a space.

    I am not be able to capture the "l" or "ml" when I am trying to build a function, or it is giving me an escape error.

    I tried:

    def stnd(text):

    text = re.sub('^l%',' liter', text)
    text = re.sub('^ml%',' milliliter', text)

    text = re.sub('^\d+\.\d+\s*l$','^\d+\.\d+\s*liter$', text)
    text = re.sub('^^\d+\.\d+\s*ml$%','^\d+\.\d+\s*milliliter$', text)

    return text

    Continue reading...

Compartilhe esta Página