vendredi 26 novembre 2021

How to extract the numeric coordinates(x,y,z) of this text string with regex

input_coords = "Some_text: 108,47,136" #case 1
input_coords = "Some_textjadd: 108 , 47 , 136" #case 2
input_coords = "Some_text: 108.5,47.9,136.6" #case 3
input_coords = "Ssssome_text: 180,,136" #case 4
input_coords = "Someddd_text: 180,47,13s6" #case 5 - FAIL case

and that in cases 1, 2 and 3 extract the coordinates as floats

float x = 108
float y = 47
float z = 136

or

float x = 108.5
float y = 47.9
float z = 136.6

but in case 4 where one of the coordinates is empty, I thought to use a replace to replace the missing coordinate with a 0 avoiding a None value

float x = 108.5
float y = 0
float z = 136.6

And case 5 would be the only one where the regex would not be fulfilled and would enter an exception, since the idea is that the regex detects if one of the coordinate values cannot be converted to a number


input_coords = "Someddd_text: 180,47,13s6" #case 5 - FAIL case

while(True):
    if():
          REGEX IF CONDITION

    else:

          float x = none or previuos value
          float y = none or previuos value
          float z = none or previuos value


Aucun commentaire:

Enregistrer un commentaire