Hello, @adam-labuš, @alan-kilborn, @m_andre_z_eckenrode and All,
Ah, OK ! With your new specifications, it’s easier to get the right regex, at once ;-))
But when you said :
For the other lines I want to delete everything else except lines that looks like this
;LAYER:1
We don’t know if you speak of lines which begin, contain or end with ;LAYER:1 ! I suppose that it is which begin…
Then, from this simple example, below :
G1 X184.218 Y123.524 E1.81219
G1 X183.179 Y125.34 E1.83974
M204 S625
;LAYER:1 bla bla blah
M205 X10 Y10
G0 F2571.4 X182.84 Y125.092
M204 S500
G1 X183.179 Y125.34 E1.83974
The following regex S/R
SEARCH (?-si)^G[01].+(X\d+\.\d+\x20Y\d+\.\d+).*|^(?!;LAYER:1).+\R
REPLACE \1
would output this text :
X184.218 Y123.524
X183.179 Y125.34
;LAYER:1 bla bla blah
X182.84 Y125.092
X183.179 Y125.34
Notes :
The in line modifiers (?-si) mean that :
Any dot regex symbol represents a single standard character and not EOL chars, as \r or \n (?-s)
The search is run in a non-insensitive way (?-i)
Then the search regex contains two alternatives, separated with the | symbol :
The regex ^G[01].+(X\d+\.\d+\x20Y\d+\.\d+).*
The part ^G[01] looks for strings G0 or G1, beginning current line
The part .+ represents any non-null range of standard characters
The part (X\d+\.\d+\x20Y\d+\.\d+) search for the exact letter X, followed with some digits \d+, followed with a literal dot \. followed by decimal digits \d+ and a space char \x20 then the exact letter Y and … ( same process than for X ). As all the part is surrounded by parentheses, its contents are stored as group 1
Finally, the part .* represents any range of standard characters, possibly null
The regex ^(?!;LAYER:1).+\R
The part ^(?!;LAYER:1) search all the lines which not begin with the string ;LAYER:1
And the part .+\R catches all the characters of any non-null line .+, with its line-break chars \R ( = \r\n for Windows files, \n for Unix files, or \r for Mac files )
In replacement :
If the left regex matches, then the group 1 is defined and corresponds to the XY coordinates, which must be rewritten => \1
Else, the right regex is used. As no group is defined, in the second alternative, the pointer to group 1 \1 return a null string. So, the current matched line is just deleted !
Best Regards,
guy038