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

Time:11-26

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


CodePudding user response:

I think this is what you're after: https://regex101.com/r/En9qFP/1/

# import the necessary packages
import re

input_coords = ["Some_text: 108,47,136",
                "Some_textjadd: 108 , 47 , 136",
                "Some_text: 108.5,47.9,136.6",
                "Ssssome_text: 180,,136",
                "Someddd_text: 180,47,13s6"]

for item in input_coords:
  m = re.compile(r"(^. :)[0-9,.\s] $", re.IGNORECASE)

  g = m.match(item)
  if g:
    print (item)
  else:
    print ("FAIL: "   item)

    # Some_text: 108,47,136
    # Some_textjadd: 108 , 47 , 136
    # Some_text: 108.5,47.9,136.6
    # Ssssome_text: 180,,136
    # FAIL: Someddd_text: 180,47,13s6

CodePudding user response:

I tried to figure out a RE that would handle this but failed to do so. However, I can offer you this:

input_coords = ["Some_text: 108,47,136",
                "Some_textjadd: 108 , 47 , 136",
                "Some_text: 108.5,47.9,136.6",
                "Ssssome_text: 180,,136",
                "Someddd_text: 180,47,13s6"]

def getcoordinates(s):
    coords = []
    for v in s.split(',')[-3:]:
        if v:
            coords.append(float(v.split()[-1]))
        else:
            coords.append(0.0)
    return coords

for s in input_coords:
    print(getcoordinates(s))

NOTE: when we try to parse '13s6' a ValueError exception will be raised

  • Related