Menu

Help converting Notepad++ format to Pythonscript

Help
2018-07-06
2018-07-20
  • Andrew Clark

    Andrew Clark - 2018-07-06

    I am trying to create a Pythonscript program to convert old CNC programs to a newer format. I am using the editor.rereplace() format to do this and have most of my functions working this way. I am having trouble with one specific format. In Notepad++ I would use Find/Replace with the following conditions to achieve what I want, line by line.

    Find: ((.?))|(a-z([-#]?\d(.\d)?))\s
    Replace: $1$2

    This removes any spaces in the program that are NOT inside of parenthesis and that would otherwise make every other function more difficult. I cannot seem to get that Find function working in Pythonscript. I have tried adding enumerable escapes ( \ ) and nothing seems to be working for me. Can anyone help me with the proper formatting to make this work? Thanks!

     
  • Sasumner

    Sasumner - 2018-07-08

    Can you post a few lines of "before" and "after"? I tried your Find regex in N++, and it just did "odd things" to some text I created, so I think I must have a misunderstanding somewhere about what you are trying to achieve.

     
  • Andrew Clark

    Andrew Clark - 2018-07-09

    Here is a quick and dirty example.

    Before:

    o1234
    (EXAMPLE PROGRAM)
    (THIS IS A CNC COMMENT)
    (I WANT THESE LINES TO RETAIN SPACES)
    ()
    G28 G91 Z0
    T1 M6 (EXAMPLE TOOL)
    M3 S10000
    G90 G0 G95 G54 X1.0 Y2.0
    G43 Z0.25 H1 D1 M8
    G1 Z-1.0 F0.006
    X-1.0 Y-2.0
    Y2.0
    X1.0
    G0 Z1.0 M5
    G28 G91 Z0 M9
    M01 (CHECK PART)
    

    After:

    o1234
    (EXAMPLE PROGRAM)
    (THIS IS A CNC COMMENT)
    (I WANT THESE LINES TO RETAIN SPACES)
    ()
    G28G91Z0
    T1M6(EXAMPLE TOOL)
    M3S10000
    G90G0G95G54X1.0Y2.0
    G43Z0.25H1D1M8
    G1Z-1.0F0.006
    X-1.0Y-2.0
    Y2.0
    X1.0
    G0Z1.0M5
    G28G91Z0M9
    M01(CHECK PART)
    

    So the goal is to remove every space in the program that is not within parenthesis. This "normalizes" the program so that all of my other regex's work without needing special clauses for spaces. This doesn't affect the way the program is read by the CNC machine and isn't really even a nuisance for the people working with the file as we're used to seeing programs both ways (with and without spaces). Hopefully that makes sense?

     
    • Jörg Hollenhorst

      Hi Andrew,

      that's pretty easy if you know the right Built-In-String Operation. The <string>.replace(<old>, <new>) operation helps you most.

      import re
      
      def match_found(m):
          myString = m.group()
          return myString.replace(' ', '£')
      
      # Send terms in parentheses to function for protecting spaces inside with £-sign.
      editor.rereplace('\([^\(]+\)', match_found)
      
      # Delete all spaces. \h is the regex expression for all horizontal spaces, tabs and so on.
      editor.rereplace('\h', '')
      
      # Substitute £-sign with spaces
      editor.rereplace('£', ' ')
      
       
  • Andrew Clark

    Andrew Clark - 2018-07-11

    Maybe not so easy huh?

     
  • Sasumner

    Sasumner - 2018-07-12

    I tried posting some code here but it won't let me. This forum is kinda dead anyway. Why don't you repost your question here?: https://notepad-plus-plus.org/community

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.