We have two kind of coordinates in our project. The normal one with x, y and z
Example:
101, 520, 62
960.93 764.22 59.20
And the extended version with 6 digits (2x xyz for position and rotation) Example:
101 520 62 3 0 0
960.93 764.22 59.20 -0.34 0.00 -89.81
They can be negative, they can be floats and can be rounded numbers. They can be separated by comma or by nothing
Using python, I am trying to find any coordinates in a string.
Example:
textbefore 101, 520, 62
GOTO 960.93 796.22 59.20 -0.34 0.00 -89.81
$5GOTO 1960.93 1796.22 159.20 -0.34 0.00 -89.81
501, 513, 162
1040, 1040, 520 text after
error
222, 222
1500, 1500, 60 (1)
1337 1337 65
124.5, 133.6, 35.4
15:13:26 Condition: index_ != StringList::npos [line 178](125, 157, 215)
Allocating shadow map cache 6324 x 6324: 76.28 MB
In the perfect world the output should be:
101 520 62
960.93 796.22 59.20 -0.34 0.00 -89.81
1960.93 1796.22 159.20 -0.34 0.00 -89.81
501 513 162
1040 1040 520
1500 1500 60
1337 1337 65
124.5 133.6 35.4
125 157 215
The last line with "Allocating shadow maps, is a bit tricky and if this fails and gets listed as coordinate, its fine.
I used this code here, which filters the numbers very good, then I was checking for 6 or 3 numbers, but I have problems with lines which have more numbers. So I need somehow a logic which checks if there numbers are "close" to each other or even separated by words.
re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", line)
If possible the code should work on Python 2.7 (Sadly we are far behind).
Thanks
You can use below regex for this
(?:(?:[+-]?\d+\.?\d*[ ,]+){5}[+-]?\d+\.?\d*)|(?:(?:[+-]?\d+\.?\d*[ ,]+){2}[+-]?\d+\.?\d*)
This will search for 2/5 consecutive numbers with ,
or space
delimited and a 3rd/6th number with a non-digit value.
Here is a demo.
Output
['101, 520, 62',
'960.93 796.22 59.20 -0.34 0.00 -89.81',
'1960.93 1796.22 159.20 -0.34 0.00 -89.81',
'501, 513, 162',
'1040, 1040, 520',
'1500, 1500, 60',
'1337 1337 65',
'124.5, 133.6, 35.4',
'125, 157, 215']