Community
    • Login

    Insert a string after each number of words with conditions

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    19 Posts 5 Posters 12.7k Views 2 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.
    • Alan KilbornA Online
      Alan Kilborn @guy038
      last edited by

      @guy038

      Yes, in playing around with your original regex, I didn’t worry about the resulting space at the end of the line, as I have my “save” shortcut mapped to “trim trailing spaces” + “save”. The ONLY way files should be saved (for me!).

      1 Reply Last reply Reply Quote 1
      • Alan KilbornA Online
        Alan Kilborn @guy038
        last edited by

        @guy038

        And it is great that you have Admin rights here and can edit old posts, but I’m neutral on this. I think that old posts should not be edited and clarifying posts should just be added on. It is difficult to follow sometimes when history is CHANGED rather than simply CORRECTED/CLARIFIED later. :-D

        1 Reply Last reply Reply Quote 0
        • guy038G Offline
          guy038
          last edited by

          Hi, Alan,

          Yes, you’re right about it : I should have created a new post with the corrections, for a better history ! It’s just that my updated post was, still, quite long and I thought it would be more clear to, simply, change my initial post. But, I do understand your point of view !

          Cheers,

          guy038

          1 Reply Last reply Reply Quote 0
          • hu maH Offline
            hu ma @Scott Sumner
            last edited by

            @Scott-Sumner
            Thanks for the info!

            1 Reply Last reply Reply Quote 0
            • guy038G Offline
              guy038
              last edited by guy038

              Hello, @hu-ma and All

              To complete the @scott-sumner post, about the two syntaxes of the searched groups, in replacement :

              • \N, with 0 < N < 10

              • $N, with 0 <= N < 2,147,483,648

              There is the other practical syntax, below :

              • ${N}, with 0 <= N < 2,147,483,648

              Indeed, let’s imagine the original text:

              abcd
              1234
              WXYZ
              

              and the first S/R :

              SEARCH ^.(..).

              REPLACE $100|

              You obtain the simple text :

              |
              |
              |
              

              Why ?! Just because, in replacement, the regex engine is looking for the group $100, which, obviously, does not exist ! So, the regex engine rewrites a zero-length string, for the non-existent group 100, followed by the literal character | !

              Now, compare, with the second S/R, below :

              SEARCH ^.(..).

              REPLACE ${1}00|

              This time, you, correctly, get the text, below :

              bc00|
              2300|
              XY00|
              

              => All the changed lines begin by the second and third characters of the original lines of text ( $1 ), and are, simply, followed by the string 00|

              Best Regards,

              guy038

              1 Reply Last reply Reply Quote 2
              • abuali humaA Offline
                abuali huma @guy038
                last edited by

                • Sorry for pumping up old thread, but my issue is related to this one.

                Cutting to the thread…
                Look at Result#2 with desired arrangement

                -Example# 2
                Greetings My Liege! As your personal advisor [NEWLINE] , I am qualified to assist you in all[NEWLINE] matters related to ruling our civilization.[NEWLINE] I am at your service.
                
                --------
                +Seeked arrangement
                I am at your service. [NEWLINE] matters related to ruling our civilization.[NEWLINE], I am qualified to assist you in all[NEWLINE]Greetings My Liege! As your personal advisor
                

                I asked before for a way to rearrange the groups between [NEWLINE] to be backward… Now I’m asking for the same but in more automated way…

                Because not all lines have the same amount of Groups, I want to arrange all the lines that contains Groups between [NEWLINE] to be backward arrangement.

                -Example#3 Contains SIX groups
                One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six
                
                -------
                +Seeked arrangement
                 six[NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE]One
                

                While using the same regex or python script

                -Example#4 Contains 4 groups 
                I want [NEWLINE] this [NEWLINE] to be  [NEWLINE] last
                
                ------
                +Seeked arrangement
                 last[NEWLINE] to be [NEWLINE] this [NEWLINE]I want 
                
                1 Reply Last reply Reply Quote 0
                • guy038G Offline
                  guy038
                  last edited by guy038

                  Hi, @abuali-huma,

                  I found a general method, which uses three consecutive S/R. We’ll need two dummy characters, NOT used in the current file. I, personally, chose the # and @ characters, but any other may be used !

                  • The first S/R :

                    • Changes any string [NEWLINE], possibly preceded and/or followed with a space character, by the dummy character #

                    • Adds, also, a # character at the end of any non-blank line

                  • The second S/R is the main S/R, which rewrites the different parts, between the # character, in reverse order.

                    • Note that this S/R will have to be performed as many times, till the message Replace All: 0 occurrences were replaced occurs, in the Replace dialog

                    • The general idea, about this S/R, is to switch the beginning and ending parts of the found text, adding a @ character, at the end of the exchanged parts, in order that the next run of this S/R, will avoid these moved parts of text ! Hence, the decreasing number of occurrences found, till zero :-))

                  • The Third S/R :

                    • Changes the # character, possibly preceded by a @ character, inside text, by the string [NEXLINE], preceded and followed with a space character

                    • Deletes the # character, possibly preceded by a @ character, when located at the end of the lines

                  All these S/R will use the Regular expression search mode, the Wrap around option and the Replace All button, of the Replace dialog

                  So, let’s start with the original text, below :

                  One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six [NEWLINE] seven [NEWLINE] eight [NEWLINE] nine [NEWLINE] ten [NEWLINE] eleven [NEWLINE] twelve
                  One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six [NEWLINE] seven [NEWLINE] eight [NEWLINE] nine [NEWLINE] ten [NEWLINE] eleven
                  One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six [NEWLINE] seven [NEWLINE] eight [NEWLINE] nine [NEWLINE] ten
                  Other text NOT concerned
                  by this Search Replacement
                  One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six [NEWLINE] seven [NEWLINE] eight [NEWLINE] nine
                  One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six [NEWLINE] seven [NEWLINE] eight
                  One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six [NEWLINE] seven
                  Bla bla blah
                  Bla bla blah
                  Bla bla blah
                  One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six
                  One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five
                  One [NEWLINE] two [NEWLINE] three [NEWLINE] four
                  Dummy text
                  inserted, in between !
                  One [NEWLINE] two [NEWLINE] three
                  One [NEWLINE] two
                  One
                  I want [NEWLINE] this [NEWLINE] to be  [NEWLINE] last
                  

                  After running the following S/R , once :

                  SEARCH (?-s)\x20?\[NEWLINE\]\x20?|(?<=.)$

                  REPLACE #

                  You should get the text, below :

                  One#two#three#four#five#six#seven#eight#nine#ten#eleven#twelve#
                  One#two#three#four#five#six#seven#eight#nine#ten#eleven#
                  One#two#three#four#five#six#seven#eight#nine#ten#
                  Other text NOT concerned#
                  by this Search Replacement#
                  One#two#three#four#five#six#seven#eight#nine#
                  One#two#three#four#five#six#seven#eight#
                  One#two#three#four#five#six#seven#
                  Bla bla blah#
                  Bla bla blah#
                  Bla bla blah#
                  One#two#three#four#five#six#
                  One#two#three#four#five#
                  One#two#three#four#
                  Dummy text#
                  inserted, in between !#
                  One#two#three#
                  One#two#
                  One#
                  I want#this#to be #last#
                  

                  After running the following S/R, SEVEN times, one after another :

                  SEARCH (?-s)([^@#\r\n]+?)#(.+#)?([^@#\r\n]+)#

                  REPLACE \3@#\2\1@#

                  The modified text is, now :

                  twelve@#eleven@#ten@#nine@#eight@#seven@#six@#five@#four@#three@#two@#One@#
                  eleven@#ten@#nine@#eight@#seven@#six#five@#four@#three@#two@#One@#
                  ten@#nine@#eight@#seven@#six@#five@#four@#three@#two@#One@#
                  Other text NOT concerned#
                  by this Search Replacement#
                  nine@#eight@#seven@#six@#five#four@#three@#two@#One@#
                  eight@#seven@#six@#five@#four@#three@#two@#One@#
                  seven@#six@#five@#four#three@#two@#One@#
                  Bla bla blah#
                  Bla bla blah#
                  Bla bla blah#
                  six@#five@#four@#three@#two@#One@#
                  five@#four@#three#two@#One@#
                  four@#three@#two@#One@#
                  Dummy text#
                  inserted, in between !#
                  three@#two#One@#
                  two@#One@#
                  One#
                  last@#to be @#this@#I want@#
                  

                  Seven consecutive runs of that regex S/R are required, to get the sought text :

                  • Run 1 : 12 occurrences replaced
                  • Run 2 : 10 occurrences replaced
                  • Run 3 : 7 occurrences replaced
                  • Run 4 : 5 occurrences replaced
                  • Run 5 : 3 occurrences replaced
                  • Run 6 : 1 occurrences replaced
                  • Run 7 : 0 occurrences replaced

                  Note : After each run, you may hit the Find Next button, before hitting the Replace All button, to guess the general process !

                  The part [^@#\r\n], in the searched regex, represents any single character, different from @, #, \n and \r


                  Then, after running the last S/R, once :

                  SEARCH (?-s)(@?#)(?=.)|@?#

                  REPLACE ?1\x20[NEWLINE]\x20

                  We obtain our final text :

                  twelve [NEWLINE] eleven [NEWLINE] ten [NEWLINE] nine [NEWLINE] eight [NEWLINE] seven [NEWLINE] six [NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
                  eleven [NEWLINE] ten [NEWLINE] nine [NEWLINE] eight [NEWLINE] seven [NEWLINE] six [NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
                  ten [NEWLINE] nine [NEWLINE] eight [NEWLINE] seven [NEWLINE] six [NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
                  Other text NOT concerned
                  by this Search Replacement
                  nine [NEWLINE] eight [NEWLINE] seven [NEWLINE] six [NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
                  eight [NEWLINE] seven [NEWLINE] six [NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
                  seven [NEWLINE] six [NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
                  Bla bla blah
                  Bla bla blah
                  Bla bla blah
                  six [NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
                  five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
                  four [NEWLINE] three [NEWLINE] two [NEWLINE] One
                  Dummy text
                  inserted, in between !
                  three [NEWLINE] two [NEWLINE] One
                  two [NEWLINE] One
                  One
                  last [NEWLINE] to be  [NEWLINE] this [NEWLINE] I want
                  

                  The search part looks for the regex @?#, either, inside the lines ( case group 1 defined ) or at end of lines ( case NO group 1 )

                  The replacement part means that, IF group 1 exists, the searched text is replaced by the string [NEWLINE], surrounded by space characters, ELSE NO replacement occurs

                  Et voilà !

                  Best Regards,

                  guy038

                  1 Reply Last reply Reply Quote 0
                  • abuali humaA Offline
                    abuali huma
                    last edited by

                    Thanks very much!
                    But just to be clear, in the first regex

                    SEARCH (?-s)\x20?[NEWLINE]\x20?|(?<=.)$

                    REPLACE #

                    Removing the value ** \x20 ** will result this
                    SEARCH (?-s)?[NEWLINE]?|(?<=.)$

                    Which will result capturing the space “if available” before and after [NEWLINE] string in first and last group?

                    1 Reply Last reply Reply Quote 0
                    • abuali humaA Offline
                      abuali huma
                      last edited by

                      I found out the removing the \x20 does what I described… Thanks again

                      1 Reply Last reply Reply Quote 0
                      • abuali humaA Offline
                        abuali huma
                        last edited by

                        I modified the original Search regex, as it catches some Unicode characters with will break the line in a middle of a word. So in the modified regex I replace \W with \x20 (space character)… so far no word breaking issues
                        Here is the modified one
                        (?-s).{1,44}(?=\x20)

                        1 Reply Last reply Reply Quote 0

                        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