Community
    • Login

    Regex: Put all sentences from Column\Tabs on a single Line

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    10 Posts 2 Posters 3.5k 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.
    • Vasile CarausV
      Vasile Caraus
      last edited by

      hello. I have 2 columns or maybe two sentences separated by tabs. Every sentence is separated by tab and divided under one another.

      To understand much better my problem, please take a look at the print screen.

      https://snag.gy/eqfgTD.jpg

      So, I want to put those 2 sentence in one single line, after the other, without being separated by tabs or space.

      I try this regex, but it is not very good, because mix them in instead to put side by side.

      Search:
      \s+(.*?)
      Replace by:
      leave a space

      Can anyone help me with another solution?

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

        Hello, Vasile,

        The screenshot of your text before replacement gives us a clue but we lack a second screenshot about the text needed, after replacement !!

        Anyway, I tried to guess it !

        So, starting with the original text, below, with a single tabulation character between the two parts of each line

        Part 1 Line 1	Part 1 Line 2
        Part 2 Line 1	Part 2 Line 2
        
        
        The part 1 of Line 5	Line 6 Part 1
        The part 2 of Line 5	Line 6 Part 2
        
        First part of Line 8	Beginning of Line 9
        Second part of Line 8	End of Line 9
        

        And, applying the following S/R :

        SEARCH (?-s)(^.*)\t+(.*)\R(.*)\t+(.*)

        REPLACE \1\3\r\n\2\4

        should give you the modified text, below, without any tabulation character :

        Part 1 Line 1Part 2 Line 1
        Part 1 Line 2Part 2 Line 2
        
        
        The part 1 of Line 5The part 2 of Line 5
        Line 6 Part 1Line 6 Part 2
        
        First part of Line 8Second part of Line 8
        Beginning of Line 9End of Line 9
        

        Notes :

        • The search regex considers two consecutive lines, which contains, each, at least one tabulation character as a separator

        • In replacement, these four parts are, simply, re-organized, according to the template 12-34 => 13-24

        • If you’re dealing with Unix files, the replacement zone should be \1\3\n\2\4

        Cheers,

        guy038

        1 Reply Last reply Reply Quote 0
        • Vasile CarausV
          Vasile Caraus
          last edited by

          hello, guy038. I have always problem with S/R: Nothing works with this \R.

          Please take a look at this print screen:

          https://snag.gy/LFDrXI.jpg

          1 Reply Last reply Reply Quote 0
          • Vasile CarausV
            Vasile Caraus
            last edited by

            or, maybe, instead of \t+ should use [\t+ ].

            I tried Search (?-s)[\t+ ](.*)\R(.*)[\t+ ](.*) and replace cu \1\3\n\2\4, nothing happens.

            1 Reply Last reply Reply Quote 0
            • Vasile CarausV
              Vasile Caraus
              last edited by

              you ask about a screen shot, what it should should look after change. I modify the file, and below you have a print screen.

              BEFORE: https://snag.gy/eqfgTD.jpg

              AFTER: https://snag.gy/bzWi4S.jpg

              1 Reply Last reply Reply Quote 0
              • Vasile CarausV
                Vasile Caraus
                last edited by Vasile Caraus

                by the ways, I was wrong. those are spaces (not tabs) in between sentences. Or both

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

                  Hi, Vasile

                  First of all, I don’t think that you have a problem with the \R syntax. May be, it was just because your initial text did not contain any tabulation character. Note that you got the error message Can’t find the text … and not the message invalid regular expression !!

                  Anyway, as from the screenshoots, you seems to use Windows files, you may, also, replace any \R sub-regex by the simple regex \r\n


                  From your last posts, I can, now, figure your problem out ! So, I suppose that the separator, between the two parts of a line, is, either :

                  • One or more Tabulation character(s)

                  • Two or more Space characters

                  Therefore, placed in a non-capturing group, the regex, detecting this separator, is (?:\t+| {2,})


                  So, from the original text, slightly modified, below :

                  Part 1 Line 1			Part 1 Line 2
                  Part 2 Line 1	Part 2 Line 2
                  
                  
                  The part 1 of Line 5    Line 6 Part 1
                  The part 2 of Line 5  Line 6 Part 2
                  
                  First part of Line 8	Beginning of Line 9
                  Second part of Line 8   End of Line 9
                  

                  The following S/R, below :

                  SEARCH (?-s)(^.*?)(?:\t+| {2,})(.*)\R(.*?)(?:\t+| {2,})(.*)

                  REPLACE \1 \3 \2 \4

                  would produce the new text below :

                  Part 1 Line 1 Part 2 Line 1 Part 1 Line 2 Part 2 Line 2
                  
                  
                  The part 1 of Line 5 The part 2 of Line 5 Line 6 Part 1 Line 6 Part 2
                  
                  First part of Line 8 Second part of Line 8 Beginning of Line 9 End of Line 9
                  

                  NOTES :

                  • In the original example text, the separator is :

                    • 3 Tabulation characters in Line 1
                    • 1 Tabulation character in Line 2
                    • 4 Space characters in Line 5
                    • 2 Space characters in Line 6
                    • 1 Tabulation character in Line 8
                    • 3 Space characters in Line 9
                  • The search regex considers a block of two consecutive lines

                  • If the symbol | represents the separator between the two parts of a line, this S/R changes the two lines :

                  1|2
                  3|4

                  into the single line :

                  \1 \3 \2 \4, with a space character between the four parts

                  Best Regards,

                  guy038

                  1 Reply Last reply Reply Quote 0
                  • Vasile CarausV
                    Vasile Caraus
                    last edited by

                    SUPER ANSWER, guy038. !! WORKS ! Thanks a lot !

                    I, also, have a little problem. The same problem, but on a longer text file. Actually, this was my concern, and I believe the solution was easier.

                    So, please see this longer text file. The same problem.

                    http://textuploader.com/dt6m8

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

                      Hello Vasile,

                      Thank you for your uploaded text ! The site textuploader.com seems to be worth looking at :-))

                      After copying your text with the usual CTRL + C/ CTRL + V action, I noticed two facts :

                      • Sometimes, words are separated by two or three space characters and strings, as, for instance, “• se obţin” contains also three space characters. So, I think it’s better to define the separator, between the two parts of each line, as, either :

                        • Five or more consecutive space characters

                        • One or more consecutive tabulation character(s)

                      To visually see this separator, just open the Search > Mark… dialog and, in the Find what: zone, type the regex \t+| {5,}, choose the Regular expression search mode and click on the Mark All button

                      -Secondly, from your text, on textuploader.com, the fourth line, from the end ( ending with pe sine este însă mai, is followed, after copy, with a space character, instead of the two characters \r\n ( Windows End of Line ) !. So I replaced the space character, after the word mai, by a normal line break ( CRLF )


                      The general method is not too difficult to figure out !

                      Let suppose I represent a five spaces separator by the … symbol and a tabulation separator with the | symbol.

                      Then, if I change the previous replacement regex by the regex \1 \3\t\2 \4, the original text :

                      1…2
                      3…4
                      5…6
                      7…8

                      will be changed, after a first click on the Replace All button, into :

                      1 3|2 4
                      5 7|6 8

                      And, after a second click on the Replace All button, into :

                      1 3 5 7|2 4 6 8

                      Now, the process is stopped because it remains, only, a single line. You just need to get rid of the single tabulation character and change it by a space character !

                      Note : for correct results your text must have an EVEN number of lines. If it’s not the case, just add a last dummy line ( for instance, #\t# )


                      To conclude :

                      • Place the caret at the very beginning of your text

                      • Preferably, uncheck the Wrap around option

                      • Use the regex S/R, below :

                      SEARCH (?-s)(^.*?)(?:\t+| {5,})(.*)\R(.*?)(?:\t+| {5,})(.*)

                      REPLACE \1 \3\t\2 \4

                      • Then, click on the Replace All button, SEVERAL times, till the message Replace All: 0 occurrences were replaced appears

                      • Finally, replace the single tabulation character, that remains, with a space and, eventually, get rid of each part of the dummy line, temporary added


                      Starting with your uploaded text, below :

                      fie că învaţă mult pentru a se descurca mai bine             să ne simţim ca şi cînd nu am avea nimic. Aceasta este
                      decît colegii lor de clasă, fie că ajung să adopte              forţa produsului final numit valoare.
                      o atitudine de capitulare, deoarece nu cred că ar            Ce sînt valorile? Sînt convingerile noastre individua-
                      putea avea şanse de reuşită.                                            le în legătură cu ceea ce este cel mai important pentru noi.
                      Într-o societate modernă învăţarea competitivă se              Valorile noastre reprezintă sisteme proprii de consideraţie
                      cuvine a fi înlocuită printr-o învăţare prin cooperare,          despre bine şi rău, corect şi incorect, lucruri de care avem
                      unde:                                                                                  nevoie în mod fundamental ca să mergem înainte. În caz
                      se lucrează împreună pentru a realiza obiective              contrar, nu ne vom simţi oameni integri şi împliniţi.
                      şi pentru a îndeplini sarcini comune;                                Sistemul de valori al şcolii contemporane este unul
                      •   se înţelege că obiectivele pot fi atinse doar dacă         complex şi nici pe departe perimat. Atîta doar că ritmul
                      şi ceilalţi membri ai grupului reuşesc s-o facă;                  şi dinamica momentului impun flexibilizarea cadrului
                      •   se obţin rezultate benefice pentru întregul grup;           axiologic cu concepte care ar face conexiuni directe între
                      •   rezultatul fiecăruia depinde într-o anumită                    „teoria” din sala de clasă şi „practica” existenţei
                      măsură de succesul altor membri ai grupului.                   cotidiene. Succesul ca valoare intrinsecă lumii noastre
                      Într-o eră a schimbărilor permanente, a supra-                 concurenţiale este o idee agreată în mediul educaţional.
                      solicitării noului, şcolile trebuie să fie în stare să-şi              Însă valenţele practice ale noţiunii date - filozofice şi
                      definească  priorităţile,  alegînd  între  o  strategie             pragmatice în acelaşi timp - urmează a fi descoperite zi
                      competitivă sau obiectivele inovatoare şi adaptarea            de zi atît de către profesori cît şi de elevi. Fiecare îşi
                      acestor transformări la necesităţile şi aspiraţiile proprii,         stabileşte propria traiectorie a Succesului, întrucît nu în
                      creînd şi condiţii interne respective. Pentru a ne schimba         zadar se afirmă: „Cel care află multe de la alţii, poate
                      cu adevărat, pentru a ne dezvolta şi prospera, trebuie să          deveni învăţat, cel care se înţelege pe sine este însă mai
                      devenim perfect conştienţi de regulile noastre şi ale          inteligent. Cel care îi ţine sub control pe alţii poate fi
                      altora, precum şi de felul în care ne apreciem sau ne            puternic, cel care se stăpîneşte pe sine este însă şi mai
                      judecăm succesul sau eşecul. Altfel, putem avea totul şi       puternic” (Lao-Tsu, Tao Teh King).
                      

                      After six consecutive clicks on the Replace All button, we obtain the ONE line text, below :

                      fie că învaţă mult pentru a se descurca mai bine decît colegii lor de clasă, fie că ajung să adopte o atitudine de capitulare, deoarece nu cred că ar putea avea şanse de reuşită. Într-o societate modernă învăţarea competitivă se cuvine a fi înlocuită printr-o învăţare prin cooperare, unde: se lucrează împreună pentru a realiza obiective şi pentru a îndeplini sarcini comune; •   se înţelege că obiectivele pot fi atinse doar dacă şi ceilalţi membri ai grupului reuşesc s-o facă; •   se obţin rezultate benefice pentru întregul grup; •   rezultatul fiecăruia depinde într-o anumită măsură de succesul altor membri ai grupului. Într-o eră a schimbărilor permanente, a supra- solicitării noului, şcolile trebuie să fie în stare să-şi definească  priorităţile,  alegînd  între  o  strategie competitivă sau obiectivele inovatoare şi adaptarea acestor transformări la necesităţile şi aspiraţiile proprii, creînd şi condiţii interne respective. Pentru a ne schimba cu adevărat, pentru a ne dezvolta şi prospera, trebuie să devenim perfect conştienţi de regulile noastre şi ale altora, precum şi de felul în care ne apreciem sau ne judecăm succesul sau eşecul. Altfel, putem avea totul şi să ne simţim ca şi cînd nu am avea nimic. Aceasta este forţa produsului final numit valoare. Ce sînt valorile? Sînt convingerile noastre individua- le în legătură cu ceea ce este cel mai important pentru noi. Valorile noastre reprezintă sisteme proprii de consideraţie despre bine şi rău, corect şi incorect, lucruri de care avem nevoie în mod fundamental ca să mergem înainte. În caz contrar, nu ne vom simţi oameni integri şi împliniţi. Sistemul de valori al şcolii contemporane este unul complex şi nici pe departe perimat. Atîta doar că ritmul şi dinamica momentului impun flexibilizarea cadrului axiologic cu concepte care ar face conexiuni directe între „teoria” din sala de clasă şi „practica” existenţei cotidiene. Succesul ca valoare intrinsecă lumii noastre concurenţiale este o idee agreată în mediul educaţional. Însă valenţele practice ale noţiunii date - filozofice şi pragmatice în acelaşi timp - urmează a fi descoperite zi de zi atît de către profesori cît şi de elevi. Fiecare îşi stabileşte propria traiectorie a Succesului, întrucît nu în zadar se afirmă: „Cel care află multe de la alţii, poate deveni învăţat, cel care se înţelege pe sine este însă mai inteligent. Cel care îi ţine sub control pe alţii poate fi puternic, cel care se stăpîneşte pe sine este însă şi mai puternic” (Lao-Tsu, Tao Teh King).
                      

                      I hope that it’s the expected result which you’re waiting for :-))

                      Cheers,

                      guy038

                      1 Reply Last reply Reply Quote 0
                      • Vasile CarausV
                        Vasile Caraus
                        last edited by

                        guy038, I don’t have to say you are THE BEST ! WORKS PERFECTLY !!

                        thank you very much !

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