简体   繁体   中英

Python regex - cleaning some Gcode

Ive been battling with this for a while and would appreciate a tip! I'm stripping certain elements from a Gcode file before sending it to a 3d printer

My string is

f100G00X345.234Y234F345.5623I-2344.234J234.234F544

or similar and I watch to match (with a view to removing) the elements where the letter 'F|f' is followed by a number (float), in the string above these are:

f100 - F345.5623 - F544

My regex that is very close sofar is

(F|f).*?([^0-9|.]|\Z)

http://rubular.com/r/4GFziqlcR6

which I think (not sure) finds the F letter, and grabs everything which is either number or dot or end of sting. BUT this also includes the last character and in the sting above matches

f100G - F345.5623I - F544

So, how do I either ditch the last character form the matches or what would be a better approach.

thanks

You are matching way too much with the .*? pattern. Match just digits and dots:

[Ff][\d.]+

This matches one character in the class of F and f , followed by one or more characters in the \\d plus . class (digits or a dot).

Demo:

>>> import re
>>> example = 'f100G00X345.234Y234F345.5623I-2344.234J234.234F544'
>>> re.findall(r'[Ff][\d.]+', example)
['f100', 'F345.5623', 'F544']
>>> re.sub(r'[Ff][\d.]+', '', example)
'G00X345.234Y234I-2344.234J234.234'

Note that your [^0-9|.] negative character class pattern also excludes the | symbol. Everything inside [^...] of the negative character class is excluded.

或者您可以使用拆分:

re.split(r'[^fF\d.][^fF]*', example)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM