[Python] Using regex in python to parse a string for variables names and values

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

  1. Stack

    Stack Membro Participativo

    I need some help with python regex.

    My "payload" string look like this "status=0/eStopStatus=2/version=1.0.16/runTime=005320"

    My code uses the following parse out a list of variables and values:

    variables = re.findall(r'([\w]+)=', payload)
    values = re.findall(r"[-+]?(?:\d*\.*\d+)", payload)


    My variables are parsed correctly as:

    status
    eStopStatus
    version
    runTime


    But the double . in my version value 1.0.16 trips up my values and I get

    0
    2
    1.0
    .16
    005320


    I must admit I am battling to understand the syntax of the parameters using regex for this.

    Any assistance appreciated

    Continue reading...

Compartilhe esta Página