Community
    • Login

    Help converting Notepad++ format to Pythonscript

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    16 Posts 4 Posters 4.3k Views 3 Watching
    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.
    • Claudia FrankC Offline
      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 Offline
        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 Offline
          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 Offline
            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 Offline
              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 Offline
                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 Offline
                  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 Offline
                    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 Offline
                      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 Offline
                        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

                        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                        With your input, this post could be even better 💗

                        Register Login
                        • First post
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors