Python regex - cleaning some Gcode -


ive been battling while , appreciate tip! i'm stripping elements gcode file before sending 3d printer

my string is

f100g00x345.234y234f345.5623i-2344.234j234.234f544 

or similar , watch match (with view removing) elements letter 'f|f' followed number (float), in string above these are:

f100 - f345.5623 - f544 

my regex close sofar is

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

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

which think (not sure) finds f letter, , grabs either number or dot or end of sting. includes last character , in sting above matches

f100g - f345.5623i - f544 

so, how either ditch last character form matches or better approach.

thanks

you matching way .*? pattern. match just digits , dots:

[ff][\d.]+ 

this matches 1 character in class of f , f, followed 1 or more characters in \d plus . class (digits or 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 [^0-9|.] negative character class pattern excludes | symbol. everything inside [^...] of negative character class excluded.


Comments