Given a coordinate string -- 'x, y' -- where x and y are 1-4 digits, separated by \D+:
'34, 58'
I want to able to get a tuple with each number as an integer:
(34 , 58)
Method 1: I could find the numbers, convert the tuple groups() to a list, convert the strings in that list to integers, then finally converting the list back to a tuple:
pattern = re.compile(r"(\d{1,4})\D+(\d{1,4})")
return tuple([ int(x) for x in list(pattern.search('34, 58').groups()) ])
Method 2: I could eliminate a tuple-to-list conversion with findall, but this method wouldn't be able to check if the coords are in the right format as the previous regex pattern:
pattern = re.compile('\d+')
return tuple([ int(x) for x in pattern.findall( '34, 58' )])
Method 3: Manually place each group() in a tuple
pattern = re.compile(r"(\d{1,4})\D+(\d{1,4})")
return ( int(m.search('34, 58').group(1)) , int(m.search('34, 58').group(2)) )
Is there a more efficient method? Specifically, I'm wondering if there a regex method to get an integer from a string, since \d already recognizes it as a number, right?
Thanks for any help.