0

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Or you can use split:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.