Finding multiple iterations of the same pattern in regex Python

Viewed 27

I got a .gcode file that I need to extract a comment that indicated the layer and the associate Z height of it. Something like this but think over 5000 lines long:

G0 F600 X96.619 Y131.6 Z0.2
G0 F3600 X72.87 Y57.87
G0 X72.6 Y57.6
;TIME_ELAPSED:173.127122
;LAYER:1

The idea is that it will take everything from Z0.2 to ;LAYER:1 and store it so it can be modified further.

I managed to come up with this pattern

re.findall(r"(Z\d+\.\d+)([\s\S]*)(;LAYER:\d)"

The problem is that it only find one pattern which ends up being the first instance of Z height and the last ;LAYER: comment. It ignores all the other instances of the pattern like the above example. I need this to work with any gcode file I can throw at which also add an extra step of complexity.

Any indication on where to look next would be appreciated.

0 Answers
Related