3

The issue I have is with g-code text. Need to find the line with "F150" the z lines are diffenrt values but needs to retain it, then add a line below G1 F1000. I tried regular expression

Find: (F150*)
Replace: \1\r\G1 F1000

and a few others to no avail.

G1 X277.8072 Y212.6482 Z-2.5000
G1 X277.3935 Y212.6617 Z-2.5000
G1 X276.9809 Y212.6737 Z-2.5000
G1 F150 Z-4.0000
G1 X276.9809 Y212.6738 Z-4.0000
G1 X276.5705 Y212.6846 Z-4.0000 

So end result becomes this:

G1 X277.8072 Y212.6482 Z-2.5000
G1 X277.3935 Y212.6617 Z-2.5000
G1 X276.9809 Y212.6737 Z-2.5000
G1 F150 Z-4.0000
G1 F1000
G1 X276.9809 Y212.6738 Z-4.0000
G1 X276.5705 Y212.6846 Z-4.0000 
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Jeff Hertz
  • 31
  • 1
  • Try `\bF150.*` and replace with `$0\nG1 F1000` – Wiktor Stribiżew Dec 24 '20 at 18:23
  • `(.*F150.*)` can also be used to find the string. @WiktorStribiżew is this efficient ? – Dev-vruper Dec 24 '20 at 18:43
  • @Dev-vruper The first `.*` grabs the whole line first, and then the regex engine backtracks to find `F150`. Thus, it is not efficient in this case, because all we need is to detect a line containing `F150` and add some text *after* it, so we are not really interested in the text before `F150`. – Wiktor Stribiżew Dec 24 '20 at 18:47
  • Got it. Thank you @WiktorStribiżew for clarifying. I should go and read about regex more instead of asking such silly questions. :) – Dev-vruper Dec 24 '20 at 18:52
  • 1
    @Dev-vruper This one was far from silly, a lot of people have trouble understanding how backtracking works and how to harness its power in regex. It took me time to understand that backtracking also occurs in lazily quantified patterns, see [Which one of these cases involves backtracking?](https://stackoverflow.com/a/54863813/3832970) – Wiktor Stribiżew Dec 24 '20 at 19:30

1 Answers1

3

You can use

Find What: \bF150\b.*
Replace With: $0\nG1 F1000

Details:

  • \bF150\b.* - a word boundary (\b), then a F150 substring, a word boundary, and then the rest of the line (till but not including any line break chars)
  • $0\nG1 F1000 - the whole match value ($0), a newline, and a G1 F1000 substring.

See the regex demo.

If you want to detect F150 inside a longer word, as in GDF1505, remove \b.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563