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.
    • Andrew ClarkA
      Andrew Clark
      last edited by

      @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!

      Claudia FrankC 1 Reply Last reply Reply Quote 0
      • Claudia FrankC
        Claudia Frank @Andrew Clark
        last edited by

        @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
        • Scott SumnerS
          Scott Sumner
          last edited by

          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
          • Claudia FrankC
            Claudia Frank
            last edited by

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

            Cheers
            Claudia

            Scott SumnerS 1 Reply Last reply Reply Quote 2
            • guy038G
              guy038
              last edited by guy038

              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

              Claudia FrankC 1 Reply Last reply Reply Quote 2
              • Claudia FrankC
                Claudia Frank @guy038
                last edited by

                @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
                • Claudia FrankC
                  Claudia Frank
                  last edited by

                  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
                  • Scott SumnerS
                    Scott Sumner @Claudia Frank
                    last edited by

                    @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?

                    Claudia FrankC 1 Reply Last reply Reply Quote 1
                    • Claudia FrankC
                      Claudia Frank @Scott Sumner
                      last edited by

                      @Scott-Sumner

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

                      Cheers
                      Claudia

                      1 Reply Last reply Reply Quote 2
                      • guy038G
                        guy038
                        last edited by guy038

                        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
                        • guy038G
                          guy038
                          last edited by guy038

                          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
                          • First post
                            Last post
                          The Community of users of the Notepad++ text editor.
                          Powered by NodeBB | Contributors