• Login
Community
  • Login

Help converting Notepad++ format to Pythonscript

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
16 Posts 4 Posters 3.4k 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.
  • A
    Andrew Clark
    last edited by Jul 12, 2018, 9:56 PM

    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 regex more difficult. I cannot seem to get that function working in Pythonscript. I have tried adding enumerable escapes ( \ ) and nothing seems to be working for me. Here are a few examples of before and after code.

    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)

    Notice that the spaces OUTSIDE of the parenthesis have been removed. Can anyone help me with the proper formatting to make this work? Thanks!

    C S 2 Replies Last reply Jul 13, 2018, 12:16 PM Reply Quote 0
    • C
      Claudia Frank @Andrew Clark
      last edited by Jul 13, 2018, 12:16 PM

      @Andrew-Clark

      from the given example this should do the job

      lines = editor.getCharacterPointer().splitlines()
      new_lines = []
      for line in lines:
          if '(' in line:
              pos_round_bracket = line.find('(')
              _line = line[:pos_round_bracket].replace(' ','')
              _line += line[pos_round_bracket:]
              new_lines.append(_line)
          else:
              new_lines.append(line.replace(' ',''))
      editor.beginUndoAction()
      editor.setText('\r\n'.join(new_lines))
      editor.endUndoAction()
      

      Cheers
      Claudia

      1 Reply Last reply Reply Quote 1
      • S
        Scott Sumner @Andrew Clark
        last edited by Jul 13, 2018, 12:20 PM

        @Andrew-Clark

        Since you are already in Pythonscript, why don’t you forget regex and go with “brute force”, something like this, or something similar? (admittedly this does not do the “ultimate” in error-checking, but it is just supposed to be an idea to get you on an alternate track of thinking–if that’s where you want to go…):

        new_text = ''
        inside_parens = False
        for in_ch in editor.getText():
            if not inside_parens and in_ch == ' ':
                continue
            elif in_ch == '(':
                inside_parens = True
            elif in_ch == ')':
                inside_parens = False
            new_text += in_ch
        editor.setText(new_text)
        
        1 Reply Last reply Reply Quote 1
        • A
          Andrew Clark
          last edited by Jul 13, 2018, 7:30 PM

          Well, that works fantastic! I actually set out to use a more programmatical approach to this, but found that I could do almost all of the things I wanted to with simple regex. It wasn’t until I hit the need described above that I got stuck. The regex for it worked, but I couldn’t get the code to work in Pythonscript.

          It will take some digging to follow what you did with that code, but it should help me with more advanced alterations in the future…when I get there. Thanks!

          S 1 Reply Last reply Jul 13, 2018, 7:47 PM Reply Quote 1
          • S
            Scott Sumner @Andrew Clark
            last edited by Jul 13, 2018, 7:47 PM

            @Andrew-Clark

            Not sure if you are directing that response to @Claudia-Frank or to me…but I’ll say this about the regex approach: I never understood how your regex worked live with Notepad++ – maybe the markdown syntax on this site was “stealing” some parts of it.

            If I try it as I see it above I get this as the AFTER text, which is clearly not right:

            OO11223344
            ((EEXXAAMMPPLLEE  PPRROOGGRRAAMM))
            ((TTHHIISS  IISS  AA  CCNNCC  CCOOMMMMEENNTT))
            ((II  WWAANNTT  TTHHEESSEE  LLIINNEESS  TTOO  RREETTAAIINN  SSPPAACCEESS))
            (())
            GG2288  GG9911  ZZ00
            TT11  MM66  ((EEXXAAMMPPLLEE  TTOOOOLL))
            MM33  SS1100000000
            GG9900  GG00  GG9955  GG5544  XX11..00  YY22..00
            GG4433  ZZ00..2255  HH11  DD11  MM88
            GG11  ZZ--11..00  FF00..000066
            XX--11..00  YY--22..00
            YY22..00
            XX11..00
            GG00  ZZ11..00  MM55
            GG2288  GG9911  ZZ00  MM99
            MM0011  ((CCHHEECCKK  PPAARRTT))
            

            What gives? Shouldn’t I see some \( and \) sequences in the regex?

            1 Reply Last reply Reply Quote 1
            • A
              Andrew Clark
              last edited by Jul 13, 2018, 8:16 PM

              @Scott-Sumner said:

              @Andrew-Clark

              Not sure if you are directing that response to @Claudia-Frank or to me…but I’ll say this about the regex approach: I never understood how your regex worked live with Notepad++ – maybe the markdown syntax on this site was “stealing” some parts of it.

              If I try it as I see it above I get this as the AFTER text, which is clearly not right:

              OO11223344
              ((EEXXAAMMPPLLEE  PPRROOGGRRAAMM))
              ((TTHHIISS  IISS  AA  CCNNCC  CCOOMMMMEENNTT))
              ((II  WWAANNTT  TTHHEESSEE  LLIINNEESS  TTOO  RREETTAAIINN  SSPPAACCEESS))
              (())
              GG2288  GG9911  ZZ00
              TT11  MM66  ((EEXXAAMMPPLLEE  TTOOOOLL))
              MM33  SS1100000000
              GG9900  GG00  GG9955  GG5544  XX11..00  YY22..00
              GG4433  ZZ00..2255  HH11  DD11  MM88
              GG11  ZZ--11..00  FF00..000066
              XX--11..00  YY--22..00
              YY22..00
              XX11..00
              GG00  ZZ11..00  MM55
              GG2288  GG9911  ZZ00  MM99
              MM0011  ((CCHHEECCKK  PPAARRTT))
              

              What gives? Shouldn’t I see some \( and \) sequences in the regex?

              My response was directed at Claudia. And you’re right, the code I posted above does not result in the proper result in Notepad++. Something must have been lost. I could go back and find it, but at this point my question has been answered so I am going to call this topic closed. Thank you all for the responses!

              C 1 Reply Last reply Jul 13, 2018, 10:56 PM Reply Quote 0
              • C
                Claudia Frank @Andrew Clark
                last edited by Jul 13, 2018, 10:56 PM

                @Andrew-Clark

                sorry, didn’t understand that you want a python script solution with regular expression replacement.

                regex = r'(\(.*?\))|(\w+)\s'  
                editor.rereplace(regex, lambda m: '{}{}'.format(*m.groups()))
                

                Cheers
                Claudia

                1 Reply Last reply Reply Quote 2
                • S
                  Scott Sumner
                  last edited by Jul 14, 2018, 11:59 AM

                  I am most-curious about why the OP (@Andrew-Clark) could get his regex replacement working in interactive N++ and not with Pythonscript, but perhaps that is a question that isn’t going to get answered…my guess would be not using raw string notation (the leading r in r'regex') in the PS…

                  1 Reply Last reply Reply Quote 1
                  • C
                    Claudia Frank
                    last edited by Jul 14, 2018, 1:34 PM

                    or maybe it was about how to get the match groups returned as replacement … ??

                    Cheers
                    Claudia

                    S 1 Reply Last reply Jul 16, 2018, 12:59 PM Reply Quote 2
                    • G
                      guy038
                      last edited by guy038 Jul 16, 2018, 10:44 AM Jul 16, 2018, 10:42 AM

                      Hello @andrew-clark, @scott-sumner, @claudia-frank and All,

                      I was away this weekend, to hike in the Cevennes mountains and my feet still remember theses rides ! As you can imagine, just sitting, comfortably, in front of your laptop and discussing Notepad++ matters is a real treat ;-))

                      So, Andrew, as for me, I would use, with native N++, the regex S/R, below :

                      SEARCH (?-s)(\(.+?\))|\x20

                      REPLACE ?1\1


                      Notes :

                      • As usual , the (?-s) modifier means that any regex dot character ( . ) will match a single standard char., only.

                      • Then, this regex would match :

                        • First, any shortest range of chars, between parentheses, stores it as group1 and rewrites this group as it is. ( Note that the ( and ) characters have to be escaped, with a \ symbol, to be considered as literals ! )

                        • Secondly, any space character, just ignored in replacement.

                      Cheers,

                      guy038

                      C 1 Reply Last reply Jul 16, 2018, 12:40 PM Reply Quote 2
                      • C
                        Claudia Frank @guy038
                        last edited by Jul 16, 2018, 12:40 PM

                        @guy038

                        Hi Guy, nice shortening but one question - it seems that ?1 is, for the posted example data, not needed in the replacement - what is the idea behind it?

                        Thank you and cheers
                        Claudia

                        1 Reply Last reply Reply Quote 1
                        • C
                          Claudia Frank
                          last edited by Jul 16, 2018, 12:46 PM

                          Just in case someone wants to add this into a python script it would now look like this

                          regex = r'(?-s)(\(.+?\))|\x20'  
                          editor.rereplace(regex, lambda m: '{}'.format(m.groups()[0]))
                          

                          Cheers
                          Claudia

                          1 Reply Last reply Reply Quote 2
                          • S
                            Scott Sumner @Claudia Frank
                            last edited by Jul 16, 2018, 12:59 PM

                            @Claudia-Frank said:

                            or maybe it was about how to get the match groups returned as replacement … ??

                            I don’t know…the Pythonscript docs for editor.rereplace() seem to have an adequate example of this…but considering it now maybe it could be made better?

                            C 1 Reply Last reply Jul 16, 2018, 1:08 PM Reply Quote 1
                            • C
                              Claudia Frank @Scott Sumner
                              last edited by Jul 16, 2018, 1:08 PM

                              @Scott-Sumner

                              there is always room to improve something - so, yeah - go for it. :-D

                              Cheers
                              Claudia

                              1 Reply Last reply Reply Quote 2
                              • G
                                guy038
                                last edited by guy038 Jul 16, 2018, 5:33 PM Jul 16, 2018, 5:16 PM

                                Hi, @andrew-clark, @scott-sumner, @claudia-frank and All

                                Sorry, for my late answer as I was elaborating a regex, for the tricky case, below :

                                https://notepad-plus-plus.org/community/topic/16053/how-to-ascend-numbers/3

                                Ah, how silly am I ! Of course, when the regex finds a space character ( the second alternative ), the group1 is not defined. Thus, in replacement, it simply rewrites…nothing, as group1 represents a zero-length string ;-))

                                Clever deduction, Claudia. So a possible regex is :

                                SEARCH (?s)(\(.+?\))|\x20

                                REPLACE \1

                                Note : I also changed the (?-s) modifier into (?s), which allows possible multi-lines ranges of text, between parentheses !

                                Best Regards,

                                guy038

                                1 Reply Last reply Reply Quote 2
                                • G
                                  guy038
                                  last edited by guy038 Jul 16, 2018, 8:23 PM Jul 16, 2018, 5:32 PM

                                  Hi, @andrew-clark, @scott-sumner, @claudia-frank and All

                                  Ah :-(( I should have tested my idea of changing (?-s) into (?s), before posting. Actually, in case of multi-lines range of characters, between parentheses, the suitable regex S/R is , rather :

                                  SEARCH (\([^()]+?\))|\x20

                                  REPLACE \1

                                  Hope it’s the right one, this time :-D

                                  Cheers

                                  guy038

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