Community
    • Login

    reGex Find copy and replace to a new line.

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 2 Posters 431 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Evangelos KosmasE
      Evangelos Kosmas
      last edited by Evangelos Kosmas

      Hello.
      I need help in a regex syntax to be able to transform the gcode for my CNC machine


      The following is a sample of gcode, my machine cannot read or execute.

      Numbers at the beginning of each line, are not really exist in code.
      I added them just for this post.
      AND
      Line1 means Program Start and line40 program end.
      Lines 2 to 16 are usefull for the machine.
      Lines 17 to 29 are notes for the user.
      All these LINES (1-29) have to stay as they are.

      The real code machine executes is in lines
      30 and 31
      32 and 33
      34 and 35
      36 and 37
      38 and 39

      Here is the sample code

      1. %
      2. |1 P3002
      3. |2 0
      4. |3 13
      5. |4 180.00
      6. |5 44.30
      7. |6 180.00
      8. |7 0 0 0 0
      9. |8 0
      10. |9
      11. |10
      12. |11
      13. |12
      14. |13
      15. |14
      16. :3002
      17. (-----------------------------
      18. ( |>DATE:Monday July 04 2022
      19. ( |>TIME:05:26 PM
      20. ( TOOLS USED ,T1 = Drill - 5mm Dia
      21. ( TOOLS USED ,T7 = Drill - 5.7mm Dia
      22. ( TOOLS USED ,T3 = Drill - 8mm Dia
      23. ( TOOLS USED ,T6 = Drill - 7.8mm Dia
      24. ( TOOLS USED ,T2 = Drill - 4mm Dia
      25. ( TOOLS USED ,T5 = Drill - 10mm Dia
      26. (
      27. (
      28. (
      29. (-----------------------------
      30. G172 X-74.305 Y-67.956 Z0.000 S17 E2 T1
      31. G100 X-74.305 Y-67.956 Z15.220 F2
      32. G172 X-74.305 Y-52.511 Z0.000 S17 E2 T7
      33. G100 X-74.305 Y-52.511 Z10.03 F1
      34. G172 X-74.305 Y-37.496 Z0.000 S17 E2 T3
      35. G100 X-74.305 Y-37.496 Z11.000 F3
      36. G172 X-74.305 Y-23.338 Z0.000 S17 E2 T3
      37. G100 X-74.305 Y-23.338 Z12.000 F1
      38. G172 X-74.734 Y-11.326 Z0.000 S17 E2 T9
      39. G100 X-74.734 Y-11.326 Z12.000 F4
      40. %

      Lines 30,32,34,36,38 (starting with G172) are useless. I have this regex ^G172.*\R to delete them and move the following line (starting with G100) one up.

      But in these lines (starting with G172) there is one information i must keep before deleting.

      In line30, i need the last digit wich is number 1
      or the last 2 digits T1
      In line32, i need the last digit wich is number 7
      or …T7
      In line34, last digit is number 3
      or…T3
      In line36, number 3…or T3
      In line38, number 9…or T9
      This number is always a digit between 1-9.

      I can mark them with several ways but i prefer this
      (?<=T)\d$ cause it has no effect on other lines.

      What i need is to copy digit 1 from line30, paste it to the end of the next line (line31) including a space and letter T in front of this number and then delete line30 and move 31 one line up.
      or
      to copy last two digits from line30, T1 and then paste them at the end of line32, including one space before them, delete line30, move line31 one up.
      It is also necessary to replace same way to the next lines of code.
      T1 form line30 to the end of line31
      T7 from line32 to the end of line33
      .
      .
      .

      So
      Old format was
      .
      .
      30. G172 X-74.305 Y-67.956 Z0.000 S17 E2 T1
      31. G100 X-74.305 Y-67.956 Z15.220 F2
      32. G172 X-74.305 Y-52.511 Z0.000 S17 E2 T7
      33. G100 X-74.305 Y-52.511 Z10.03 F1
      34. G172 X-74.305 Y-37.496 Z0.000 S17 E2 T3
      35. G100 X-74.305 Y-37.496 Z11.000 F3
      36. G172 X-74.305 Y-23.338 Z0.000 S17 E2 T3
      37. G100 X-74.305 Y-23.338 Z12.000 F1
      38. G172 X-74.734 Y-11.326 Z0.000 S17 E2 T9
      39. G100 X-74.734 Y-11.326 Z12.000 F4
      40. %

      And the New format must be like this
      .
      .
      30. G100 X-74.305 Y-67.956 Z15.220 F2 T1
      31. G100 X-74.305 Y-52.511 Z10.03 F1 T7
      32. G100 X-74.305 Y-37.496 Z11.000 F3 T3
      33. G100 X-74.305 Y-23.338 Z12.000 F1 T3
      34. G100 X-74.734 Y-11.326 Z12.000 F4 T9
      35. %

      Thanks in front
      Evangelos

      Terry RT 1 Reply Last reply Reply Quote 0
      • Terry RT
        Terry R @Evangelos Kosmas
        last edited by Terry R

        @Evangelos-Kosmas said in reGex Find copy and replace to a new line.:

        I need help in a regex syntax to be able to transform the gcode for my CNC machine

        As your example has been altered (by you adding line numbers) and possibly by the posting engine as you did not put the example inside of a black box (see this FAQ post) my solution may or may not work. I’m hoping it will as the regex you provided for deleting the G172 line does help.

        So using the Replace function we have:
        Find What:(?-s)^G172.*(T\d)\R(.+)
        Replace With:\2 \1 note there is a space between \2 and \1.
        So I just extended what you already had, including the T digit portion on the first line, also selecting the entire second line and then writing back the portions you wanted to keep.

        See if it works and please do come back to us either way. if you need to post further examples please read that FAQ post I linked to above.

        Terry

        1 Reply Last reply Reply Quote 2
        • Evangelos KosmasE
          Evangelos Kosmas
          last edited by

          Thank you for your direct responce.
          It works great.

          Its perfect

          Thank you again
          Evangelos

          1 Reply Last reply Reply Quote 1
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors