I've been banging my head against this for 2 days and still can't crack it.
I have Gcode files with hundreds of lines of XY position data and I'm trying to work out the regex to extract only the float values following the "X" and "Y" characters.
each line of Gcode usually likes this and is fairly simple to work with
G1X445.71Y602.47
but occasionally I have odd lines like these which make it a whole lot more tricky
G1X446.11 // no Y value
G1Y601.91 // no X value
G1X445.73Y602.26S76.5F300 // Y value is followed by S or F values
I'm try to work out the regex to extract only the X and Y values in any of these situations. the values are always positive floats. so magic regex code something like this
var lineData = "G1X445.71Y602.47";
var X = lineData.match(/regex_to_find_X_float/)
var Y = lineData.match(/regex_to_find_Y_float/)
//resulting in: X= 445.71 Y= 602.47
and for the occasional inconsistent lines
- G1X446.11 --> X= 446.11 Y=null (adds a null for the missing Y value)
- G1Y601.91 --> X= null Y= 601.91 (adds a null for the missing X value
- G1X445.73Y602.26S76.5F300 --> X= 445.73 Y=602.26 (ignores the F and S values)
the closest i've come is
X(?:\d+\.\d+|\d+)
but it still keeps the X as in "X445.71"
any suggestions very greatly appreciated...