• Login
Community
  • Login

Split lines in n characters completing whole words in Notepad++

Scheduled Pinned Locked Moved General Discussion
8 Posts 6 Posters 422 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.
  • J
    Joan Chaparro i Bertran
    last edited by Joan Chaparro i Bertran Nov 16, 2020, 3:27 PM Nov 16, 2020, 3:26 PM

    I’ve a .txt like this:

    CAS ACCIDENT PÈNDOL TIBIDABO
     SN Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc d'atraccions del Tibidabo
     BT ACCIDENTS
     RT PARCS D'ATRACCIONS
    CAS ADIF
     SN El dia 5 de Maig es fan els primers registres a les seu d'Adif per esclarir un possible cas de malversació de fons públics durant les obres de l'Ave Madrid Barcelona. L'operació es va anomenar operació yogui. També per al presumpte desviament de diners o sobrecostos no explicats que han paralitzar les obres de La Sagrera (2016)
     BT CORRUPCIÓ
    CAS AGRESSIÓ GUÀRDIA URBANA
     SN Per l'agressió que va rebre un guàrdia urbà (que està en coma) durant uns aldarulls produits arran d'una una festa il·legal a una casa okupada al carrer Sant Pere més Baix el 4F de 2006. Hi ha tres joves acusats de l'agressió. S'ha celebrat el judici el gener de 2008.
    CAS APAGADA BARCELONA
     SN 16/03/2010
     BT TALLS ELECTRICITAT
    

    I would like to match sentences starting with " SN " and then split in 220 characters not cutting whole words.

    I find this

    ^ SN .{220}
    

    for Find, but it split in the middle of words.

    Thanks

    A A 2 Replies Last reply Nov 16, 2020, 3:38 PM Reply Quote 0
    • A
      Alan Kilborn @Joan Chaparro i Bertran
      last edited by Nov 16, 2020, 3:38 PM

      @Joan-Chaparro-i-Bertran

      Hmm. Seems “underspecified”.
      Do you want to let lines where it would split in the middle of a word actually go longer than 220, or…?

      1 Reply Last reply Reply Quote 0
      • A
        astrosofista @Joan Chaparro i Bertran
        last edited by Nov 16, 2020, 3:46 PM

        Hi @Joan-Chaparro-i-Bertran

        Take a look to this other thread.

        If you want to split lines before char 220, try this regex:

        Search:  ^.{1,220}\K\h+(?=\w)
        Replace: $1\r\n
        

        Hope this helps.

        1 Reply Last reply Reply Quote 3
        • T
          Terry R
          last edited by Terry R Nov 16, 2020, 7:44 PM Nov 16, 2020, 7:43 PM

          @astrosofista said in Split lines in n characters completing whole words in Notepad++:

          If you want to split lines before char 220, try this regex:
          Search: ^.{1,220}\K\h+(?=\w)
          Replace: $1\r\n

          Unless I’m missing something obvious, my test with this showed it did not work. The issue is it will work on ANY line regardless of length. It looks for the last space before either the 220 character mark OR the end of line. So even short lines are affected by this.

          In the thread linked the discussion was about being selective over the lines being operated on, hence a lookahead to find those lines before operating on them.

          My regex would be:
          Find What:(?-is)^(?=\h*SN.{218})(\h*SN.{1,217})\h
          Replace With:$1\r\n

          As an explanation it will ONLY find lines at least as long as 220 characters. Given there might be some spaces before the “SN” the possibility exists of the line length being slightly over the 220 character mark.

          I find that although I have provided a solution I think I’m still missing something. There are possibly still some requirements the OP hasn’t provided, if so, feel free to elaborate on your request.

          Terry

          P A 2 Replies Last reply Nov 16, 2020, 8:00 PM Reply Quote 1
          • P
            PeterJones @Terry R
            last edited by PeterJones Nov 16, 2020, 8:00 PM Nov 16, 2020, 8:00 PM

            @Terry-R said in Split lines in n characters completing whole words in Notepad++:

            Given there might be some spaces before the “SN” the possibility exists of the line length being slightly over the 220 character mark.

            So make the character count independent of the SN check by two lookahead prefixes rather than putting SN and the count in one: (?-is)^(?=\h*SN)(?=.{220})(\h*SN.{1,220})\h

            My tests only find one in this instance:

            MANY PREFIX SPACE
                   SN 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
                   SN 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 short
            

            which becomes

            MANY PREFIX SPACE
                   SN 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
            123456789 123456789 123456789 123456789 123456789
                   SN 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 short
            

            so it could tell the difference between a line that didn’t have 220 chars and one that did, but still required a SN as the first non-space on the line.

            1 Reply Last reply Reply Quote 1
            • T
              Terry R
              last edited by Terry R Nov 16, 2020, 8:44 PM Nov 16, 2020, 8:44 PM

              @PeterJones said in Split lines in n characters completing whole words in Notepad++:

              So make the character count independent of the SN

              The reason I left my regex as it was is because the OP had his starting position for counting after the SN string, hence figured the leading space(s) weren’t an issue. Actually looking again as the OP’s regex I see a space between the SN and the ., so in fact the length was going to be 223 from the SN, and excluding any prefixed spaces, 1 of which the OP has also included.

              The plot thickens. I think the OP needs to elaborate further, or at least come back with, do any of the solutions meet their needs.

              Terry

              1 Reply Last reply Reply Quote 1
              • G
                guy038
                last edited by guy038 Nov 17, 2020, 11:42 AM Nov 16, 2020, 11:12 PM

                Hello @joan-chaparro-i-bertran, @astrosofista, @terry-r, @peterjones and All,

                We must not forget that lines may be 2 x 220 chars or more ! And also that a line of 220 chars, or so, may just end the line, followed with a line-break. In that case, it must not be rewritten during replacement !

                I also assume that the OP consider all chars of a line, including the leading string Space + SN + Space

                So, given this initial text, fully modified for tests :

                CAS ACCIDENT PÈNDOL TIBIDABO
                 SN Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc d'atraccions del Tibidabo
                 BT ACCIDENTS
                 RT PARCS D'ATRACCIONS
                CAS ADIF
                 SN El dia 5 de Maig es fan els primers registres a les seu d'Adif per esclarir un possible cas de malversació de fons públics durant les obres de l'Ave Madrid Barcelona. L'operació es va anomenar operació yogui. També per al presumpte desviament de diners o sobrecostos no explicats que han paralitzar les obres de La Sagrera (2016)
                 BT CORRUPCIÓ
                CAS AGRESSIÓ GUÀRDIA URBANA
                 SN Per l'agressió que va rebre un guàrdia urbà (que està en coma) durant uns aldarulls produits arran d'una una festa il·legal a una casa okupada al carrer Sant Pere més Baix el 4F de 2006. Hi ha tres joves acusats de l'agressió. S'ha celebrat el judici el gener de 2008.
                CAS APAGADA BARCELONA
                 SN 16/03/2010
                 BT TALLS ELECTRICITAT
                 SN El dia 5 de Maig es fan els primers registres a les seu d'Adif per esclarir un possible cas de malversació de fons públics durant les obres de l'Ave Madrid Barcelona. L'operació es va anomenar operació yogui. També per al presumpte desviament de diners o sobrecostos no explicats que han paralitzar les obres de La Sagrera (2016) El dia 5 de Maig es fan els primers registres a les seu d'Adif per esclarir un possible cas de malversació de fons públics durant les obres de l'Ave Madrid Barcelona. L'operació es va anomenar operació yogui. També per al presumpte desviament de diners o sobrecostos no explicats que han paralitzar les obres de La Sagrera (2016)
                CAS ACCIDENT PÈNDOL TIBIDABO
                 SN Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc d'atraccions del Tibidabo
                 BT ACCIDENTS
                CAS ADIF
                 SN El dia 5 de Maig es fan els primers registres a les seu d'Adif per esclarir un possible cas de malversació de fons públics durant les obres de l'Ave Madrid Barcelona. L'operació es va anomenar operació yogui. També per al presumpte desviament de diners o sobrecostos no explicats que han paralitzar les obres de La Sagrera (2016)
                 BT CORRUPCIÓ
                CAS AGRESSIÓ GUÀRDIA URBANA
                 SN Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc d'atraccions del Tibidabo Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc
                 BT ACCIDENTS 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 
                 RT PARCS D'ATRACCIONS
                CAS AGRESSIÓ GUÀRDIA URBANA
                 SN Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc d'atraccions del Tibidabo Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del Abcdefghijklmno
                 BT ACCIDENTS
                 RT PARCS D'ATRACCIONS
                CAS AGRESSIÓ GUÀRDIA URBANA
                 SN Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc d'atraccions del Tibidabo Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del del pèndol del Parc 12345
                 BT ACCIDENTS
                 RT PARCS D'ATRACCIONS
                CAS AGRESSIÓ GUÀRDIA URBANA
                 SN Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc d'atraccions del Tibidabo Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del del pèndol del Parc 123456        
                 BT ACCIDENTS
                 RT PARCS D'ATRACCIONS
                CAS AGRESSIÓ GUÀRDIA URBANA
                 SN Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc d'atraccions del Tibidabo Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del del pèndol del Parc 1234567
                 BT ACCIDENTS
                 RT PARCS D'ATRACCIONS
                

                The following regex S/R :

                SEARCH (?-s)(?=^\h*SN|\G)(.{190,219}[^\h\r\n])(?:\h*(?=\R)|(\h+))

                REPLACE $1?2\r\n

                Gives the expected text, below :

                CAS ACCIDENT PÈNDOL TIBIDABO
                 SN Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc d'atraccions del Tibidabo
                 BT ACCIDENTS
                 RT PARCS D'ATRACCIONS
                CAS ADIF
                 SN El dia 5 de Maig es fan els primers registres a les seu d'Adif per esclarir un possible cas de malversació de fons públics durant les obres de l'Ave Madrid Barcelona. L'operació es va anomenar operació yogui. També
                per al presumpte desviament de diners o sobrecostos no explicats que han paralitzar les obres de La Sagrera (2016)
                 BT CORRUPCIÓ
                CAS AGRESSIÓ GUÀRDIA URBANA
                 SN Per l'agressió que va rebre un guàrdia urbà (que està en coma) durant uns aldarulls produits arran d'una una festa il·legal a una casa okupada al carrer Sant Pere més Baix el 4F de 2006. Hi ha tres joves acusats de
                l'agressió. S'ha celebrat el judici el gener de 2008.
                CAS APAGADA BARCELONA
                 SN 16/03/2010
                 BT TALLS ELECTRICITAT
                 SN El dia 5 de Maig es fan els primers registres a les seu d'Adif per esclarir un possible cas de malversació de fons públics durant les obres de l'Ave Madrid Barcelona. L'operació es va anomenar operació yogui. També
                per al presumpte desviament de diners o sobrecostos no explicats que han paralitzar les obres de La Sagrera (2016) El dia 5 de Maig es fan els primers registres a les seu d'Adif per esclarir un possible cas de
                malversació de fons públics durant les obres de l'Ave Madrid Barcelona. L'operació es va anomenar operació yogui. També per al presumpte desviament de diners o sobrecostos no explicats que han paralitzar les obres de La
                Sagrera (2016)
                CAS ACCIDENT PÈNDOL TIBIDABO
                 SN Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc d'atraccions del Tibidabo
                 BT ACCIDENTS
                CAS ADIF
                 SN El dia 5 de Maig es fan els primers registres a les seu d'Adif per esclarir un possible cas de malversació de fons públics durant les obres de l'Ave Madrid Barcelona. L'operació es va anomenar operació yogui. També
                per al presumpte desviament de diners o sobrecostos no explicats que han paralitzar les obres de La Sagrera (2016)
                 BT CORRUPCIÓ
                CAS AGRESSIÓ GUÀRDIA URBANA
                 SN Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc d'atraccions del Tibidabo Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc
                 BT ACCIDENTS 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 
                 RT PARCS D'ATRACCIONS
                CAS AGRESSIÓ GUÀRDIA URBANA
                 SN Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc d'atraccions del Tibidabo Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del Abcdefghijklmno
                 BT ACCIDENTS
                 RT PARCS D'ATRACCIONS
                CAS AGRESSIÓ GUÀRDIA URBANA
                 SN Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc d'atraccions del Tibidabo Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del del pèndol del Parc 12345
                 BT ACCIDENTS
                 RT PARCS D'ATRACCIONS
                CAS AGRESSIÓ GUÀRDIA URBANA
                 SN Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc d'atraccions del Tibidabo Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del del pèndol del Parc 123456
                 BT ACCIDENTS
                 RT PARCS D'ATRACCIONS
                CAS AGRESSIÓ GUÀRDIA URBANA
                 SN Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del pèndol del Parc d'atraccions del Tibidabo Per la mort d'una jove de 14 anys, Andrea, el 17/07/2010 a l'atracció del del pèndol del Parc
                1234567
                 BT ACCIDENTS
                 RT PARCS D'ATRACCIONS
                

                Notes :

                • To be sure that the 191 to 220 chars, after a first part Sp + SN + Sp of 220 chars, or so, is the continuation of that first line, I needed to use the \G assertion, which searches a new match right after the end of the previous one !

                • Indeed, when we go to a next line, we skip the EOL characters. So, necessarily. the look-ahead (?=^\h*SN|\G) forces a new match to be right before a new line Sp + SN + Sp .... as the \G assertion is not true ( the previous match ends before the line-break and the next match begins after this line break, so the two matches are not consecutive ! )

                • For the same reason, we must place the \R syntax, near the end of the search regex, in a look-ahead to avoid matching it, explicitly !

                • To end with, I used the range of chars .{190,219} because I’m taking in account very long words, up to 29 chars. Of course feel free to change the lower limit to 200, even 210 !

                Best Regards,

                guy038

                1 Reply Last reply Reply Quote 1
                • A
                  astrosofista @Terry R
                  last edited by Nov 25, 2020, 3:06 AM

                  Hi @Terry-R

                  You’re right, my bad. I misread the whole thing. Hope that OP wasn’t fooled by my post.

                  BR

                  1 Reply Last reply Reply Quote 0
                  • A Alan Kilborn referenced this topic on Apr 3, 2025, 11:33 PM
                  • First post
                    Last post
                  The Community of users of the Notepad++ text editor.
                  Powered by NodeBB | Contributors