Community
    • Login

    New line every n characters

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    13 Posts 5 Posters 9.7k 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.
    • Terry RT
      Terry R
      last edited by

      @Alan-Kilborn said in New line every n characters:

      find: (?-s).{500}
      repl: ${0}\r\n
      Tick the Wrap around box, and press Replace All button

      I would make some changes:

      1. (?-s) is not required as no newline characters in file
      2. Untick wrap around button as important the regex starts in very first position.
      3. Cursor MUST be in first position of file BEFORE regex run.

      All because OP stated it is all serial information, so ALL on one line.

      Likely without these changes it will work out OK but if OP had moved through file first before starting the regex the cut points will be in the wrong positions.

      My 2c worth
      Terry

      Alan KilbornA 1 Reply Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @Terry R
        last edited by Alan Kilborn

        @Terry-R

        Well, I use (?-s) as a “safety measure”. If I don’t intend line-ending characters to be matched by the ., I preamble with (?-s). For me, it’s a good habit to always specify (?s) or (?-s) when using .. And, I believe it is a good habit for forum postings as well, so that the . matches newline checkbox state doesn’t have to be provided as a separate item.

        But sure, since there are no line-endings in the OP’s data…

        Untick wrap around button as important the regex starts in very first position.

        I don’t understand what is meant here. Well, I understand how to untick, but I don’t know what the rest means or how it is relevant.

        Cursor MUST be in first position of file BEFORE regex run.

        Hmmm, having Wrap around ticked negates the need for this, and is simpler than telling a user to move the caret.

        Perhaps you don’t know that a Replace All search with Wrap around ticked does one transit through the file, starting at the very top, no matter where the caret starts out at?

        Maybe you do know, but I’ll keep going for those that don’t…

        Prove it thusly:

        • Find data for both cases: abcdefghijklmno
        • Replace data for both cases: @
        • Caret starting location for both cases: between the b and the c.

        Case 1:

        • Tick Wrap around
        • Press Replace all

        Result: @klmno

        Case 2:

        • Untick Wrap around
        • Press Replace all

        Result: ab@mno

        The key point here is that for Case 1 the search matches at start of file, NOT from the caret.

        Alan KilbornA Terry RT 3 Replies Last reply Reply Quote 2
        • Alan KilbornA
          Alan Kilborn @Alan Kilborn
          last edited by Alan Kilborn

          @Alan-Kilborn said in New line every n characters:

          Prove it thusly:

          Find data for both cases: abcdefghijklmno
          Replace data for both cases: @
          Caret starting location for both cases: between the b and the c.

          Reading my own posting I am stunned to see that I left off a very important part of the instructions, applicable to both Case 1 and 2:

          • Put (?-s).{10} in the Find what box

          And of course this doesn’t make any sense unless we’re in Regular expression for the Search mode.

          1 Reply Last reply Reply Quote 1
          • Terry RT
            Terry R @Alan Kilborn
            last edited by

            @Alan-Kilborn said in New line every n characters:

            Perhaps you don’t know that a Replace All search with Wrap around ticked does one transit through the file, starting at the very top, no matter where the caret starts out at?

            Oh, no I hadn’t realised that important fact. I suppose in tests I have always identified that as a potential issue when using the Replace button. Thank you for pointing out that important fact.

            So then the wrap around being ticked is actually redundant with a Replace All button press.

            Terry

            Terry RT 1 Reply Last reply Reply Quote 0
            • Terry RT
              Terry R @Alan Kilborn
              last edited by

              @Alan-Kilborn said in New line every n characters:

              Perhaps you don’t know that a Replace All search with Wrap around ticked does one transit through the file, starting at the very top, no matter where the caret starts out at?

              I just checked the manual and there it says:
              Replace All: Replaces all matches from the active location onward (following the Backward direction and Wrap around settings as appropriate).

              That then seems to be wrong unless my interpretation is wrong of what active location means.

              Terry

              1 Reply Last reply Reply Quote 0
              • Terry RT
                Terry R @Terry R
                last edited by

                @Terry-R said in New line every n characters:

                So then the wrap around being ticked is actually redundant with a Replace All button press.

                Sorry, I had read it differently the first time.

                So Replace All with Wrap Around ticked starts at the first position in the file.
                Replace All button with Wrap Around NOT ticked starts at the current position.

                From your example I see it works. The manual does NOT appear to outline this VERY important change. At least not where Wrap Around and Replace All are described!

                Is this something that needs to be amended?

                Terry

                Alan KilbornA 1 Reply Last reply Reply Quote 1
                • guy038G
                  guy038
                  last edited by guy038

                  Hi, @terry-r, @alan-kilborn, and All,

                  Let’s consider this simple text, below, in a new Windows tab

                  123
                  456
                  789
                  

                  with the last line without CRLF. So, the exact contents are the 13 bytes 1 2 3 CR LF 4 5 6 CR LF 7 8 9


                  FIRST test :

                  • Move the caret right after digit 5

                  • Run the regex S/R :

                    • SEARCH (?s).{3}

                    • REPLACE @

                    • Untick the Wrap around option

                    • Click on the Replace All button

                  we get the text :

                  123
                  45@@
                  

                  without CRLF after the last @ char. Remember that the CR and LF characters count for ONE char because of the (?s) syntax !

                  • Then, move the caret at the very beginning of file, right before the digit 1

                  • Run the regex S/R :

                    • SEARCH (?s).{3}

                    • REPLACE @

                    • The Wrap around option is still unticked

                    • Click on the Replace All button

                  The text is now changed as :

                  @@@
                  

                  without CRLF after the last @ char

                  So, it’s the final result if we suppose that the regex engine goes from the digit 6 to the digit 9, then from digit 1 to digit 5


                  SECOND test :

                  • Move, again, the caret right after digit 5

                  • Run the same regex S/R :

                    • SEARCH (?s).{3}

                    • REPLACE @

                    • Tick the Wrap around option ( IMPORTANT )

                    • Click on the Replace All button

                  we get the text :

                  @@@@9
                  

                  without CRLF after the 9 digit

                  And this result proves that, although the caret was initially between digits 5 and 6, due to the Wrap around option ticked, the caret is internally moved at the very beginning of file and, then, the regex engine goes from digit 1 to digit 9 to process contents

                  As the total number of bytes is 13, this means that the search regex had sliced the text in 4 ranges of 3 chars, leaving the 9 digit alone, at the end ! Just logical ;-))

                  Best Regards,

                  guy038

                  1 Reply Last reply Reply Quote 0
                  • Alan KilbornA
                    Alan Kilborn @Terry R
                    last edited by

                    @Terry-R said in New line every n characters:

                    So Replace All with Wrap Around ticked starts at the first position in the file.
                    Replace All button with Wrap Around NOT ticked starts at the current position.
                    From your example I see it works.

                    My earlier postings were not so great :-(
                    But I see that you got the point.

                    I’m not sure @guy038 's posting adds anything, except as another example of proof of how it works?

                    The manual does NOT appear to outline this VERY important change. At least not where Wrap Around and Replace All are described!
                    Is this something that needs to be amended?

                    Yes, if that’s what the manual says, then I think it could be better.
                    What does @PeterJones think, as our resident manual guru?

                    BTW, a posting somewhere here long ago (I think) said to think about Wrap around ticked as meaning “Entire document” in this (Replace All) circumstance, and I’ve always remembered that, as it seems to be appropriate.

                    PeterJonesP 1 Reply Last reply Reply Quote 0
                    • PeterJonesP
                      PeterJones @Alan Kilborn
                      last edited by PeterJones

                      @Alan-Kilborn said in New line every n characters:

                      @Terry-R said in New line every n characters:

                      So Replace All with Wrap Around ticked starts at the first position in the file.
                      Replace All button with Wrap Around NOT ticked starts at the current position.

                      I disagree. AFAIK, if my cursor is on line 3 of a 5-line file, and there is a sequence that will match on all 5 lines, and I hit replace all with wrapping enabled, the first replacement will be on line 3, the second on line 4, the third on line 5, the fourth on line 1, and the fifth on line 2. I cannot think of a regex offhand that would cause that to be different than starting a replace-all at the begining of a file… but there might be one.

                      … experiment …

                      Nope, I was wrong.

                      Data:

                      one two three four five
                      fourteen something fifteen
                      

                      regex = FIND:(?i-s)\b((?<!☺ )f\w+) => REPLACE:${1}☺

                      If my original assertion, and what is implied in the manual, were true, then starting with the cursor before four vs. before five and hitting Replace All would come up with different answers. Between the four and five, it would add a smiley to five if it really started wherever the cursor was, but would only add a smiley after four if it really started from the beginning.

                      With wrap-around enabled, it only puts the smiley after the four, not the five; without wrap-around, it puts it after the five if the cursor is between.

                      Is this something that needs to be amended?

                      Yes, if that’s what the manual says, then I think it could be better.

                      What does @PeterJones think, as our resident manual guru?

                      What I thought is different than what I could prove. I thought wrong.
                      usermanual issue #178 submitted.

                      1 Reply Last reply Reply Quote 1
                      • Terry RT
                        Terry R
                        last edited by

                        @PeterJones said in New line every n characters:

                        usermanual issue #178 submitted.

                        I’ve taken this to a new discussion located here.
                        As far as the OP’s problem, this has been solved. I did not wish to further muddy these waters and have instead continued with a new post.

                        Terry

                        1 Reply Last reply Reply Quote 1
                        • Giannicola BonoraG
                          Giannicola Bonora
                          last edited by

                          I am stunned by the quantity and quality of help available in this community in a very short time! Thank you very much to everyone :)

                          1 Reply Last reply Reply Quote 3
                          • First post
                            Last post
                          The Community of users of the Notepad++ text editor.
                          Powered by NodeBB | Contributors