I’m parsing Gcode files for 3D printing. There are similar lines and I’m trying to pick the pattern (it indicates the function of the line). One problem I’ve run into is returning a positive result whether or not a parameter is in the line.
These two lines are both extrusions (squirting plastic), but one has the “F” parameter in it. The parameters will sometimes be integers, and other times they will be floats. The X, Y, and E parameters may be negative or positive.
G1 F3000 X123.45 Y123.45 E123.45678
G1 X-200.00 Y200.00 E222.34567
This pattern kind of works, but fails for the second example where the “F” parameter is missing (which is most of the time).
“G1 F(\d+.\d+|\d+) X(-?\d+.\d+|-?\d+) Y(-?\d+.\d+|-?\d+) E(-?\d+.\d+|-?\d+)”
One problem looks to be that when the “F” parameter is missing, the pattern has an extra “space” character. My workaround has been to use an IF statement, but it just looks clumsy.
I tried putting a “|” in hoping for “If F or not F” but haven’t gotten that to work.
A second part of this would be; how do I return values from (for example) the “X” parameter?
(I’ve grown to really dislike dealing with regular expressions.)