Community
    • Login

    Regex plese help - how to remove every 2nd and 3rd

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    11 Posts 3 Posters 2.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.
    • Martin XM
      Martin X
      last edited by

      Hi Friends

      please help me how to manage this
      I want to remove every send and third line in my text file without leaving a blank line.

      This is my file:

      Login: xxxx
      Lng: Java:version- xxxx
      Code: xxx
      Login: xxxx
      Lng: Java:version- xxxx
      Code: xxx
      Login: xxxx
      Lng: Java:version- xxxx
      Code: xxx
      Login: xxxx
      Lng: Java:version- xxxx
      Code: xxx
      

      And i want to see

      Login: xxxx
      Login: xxxx
      Login: xxxx
      Login: xxxx
      
      1 Reply Last reply Reply Quote 0
      • Terry RT
        Terry R
        last edited by

        @Martin-X said in Regex plese help - how to remove every 2nd and 3rd:

        I want to remove every send and third line

        As your file looks to only contain 3 types of lines, why not just sort the lines so all login lines are together. Then select all others and remove them.

        Terry

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

          @Terry-R makes a good point, but if the OP really wants to use a regex replacement, I’d suggest:

          Find what box: (?-s)^(Login:.+\R).+\R.+\R
          Replace with box: \1
          Match case checkbox: ticked
          Search mode radiobutton: Normal

          Martin XM 1 Reply Last reply Reply Quote 4
          • Martin XM
            Martin X @Alan Kilborn
            last edited by

            @Alan-Kilborn

            thank you
            your regular expression works but the last two lines remain
            Why it not remove them?

            before
            reg1.png
            after
            reg2.png

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

              @Martin-X said in Regex plese help - how to remove every 2nd and 3rd:

              Why it not remove them?

              The reason is that the regex HAS to find 3 lines, all with end of line markers \R. Unfortunately the last line has a end of file marker instead. You will note that in your example it found 3 occurrences, not 4 as you should have expected.
              Another way to achieve what you want is to use the Mark function. Type Login: and make sure “bookmark line” is ticked. As the search text is constant this search can be done in normal mode even. Once the lines are marked (generally with a blue circle at start of line) you can then remove unmarked lines by going to Search\Bookmark\Remove unmarked lines. I did notice in a quick test that this also produces a side effect of leaving a blank line at the bottom of the file.

              Terry

              PS I note @Alan-Kilborn post stated search mode normal, however you had it as regular expression. Thats good as his post had that mistake but obviously you saw past that.

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

                @Terry-R said in Regex plese help - how to remove every 2nd and 3rd:

                I note @Alan-Kilborn post stated search mode normal, however you had it as regular expression. Thats good as his post had that mistake but obviously you saw past that.

                Arrrgh. Apologies for the (obvious) mistake!

                Unfortunately the last line has a end of file marker instead.

                Yea, if we’re just talking about one file, just handle the last lines manually.
                If we’re talking thousands of files, though, you should state that initially and a solution that handles either kind of file ending can be developed.
                Or…use the editorconfig plugin and make sure all files are properly terminated with a line-ending (my preferred method).

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

                  In this instance it isn’t that difficult to craft the solution that handles a file-ending of either type:

                  Find what box: ^(Login:.+\R).+\R.+(?:\R|\z)
                  Replace with box: \1
                  Match case checkbox: ticked
                  Search mode radiobutton: Regular expression

                  Martin XM 1 Reply Last reply Reply Quote 4
                  • Martin XM
                    Martin X @Alan Kilborn
                    last edited by

                    @Alan-Kilborn

                    Whank You very much :) works like a charm.

                    But I want to ask for another example.
                    How to change this regex

                    ^(Login:.+\R).+\R.+(?:\R|\z)
                    

                    if I want to get this effect:

                    xxxx
                    xxxx
                    xxxx
                    xxxx

                    instead of:

                    Login: xxxx
                    Login: xxxx
                    Login: xxxx
                    Login: xxxx

                    Alan KilbornA 1 Reply Last reply Reply Quote 0
                    • Alan KilbornA
                      Alan Kilborn @Martin X
                      last edited by

                      @Martin-X

                      Well, we ask people to “take away a bit of learning” from every piece of advice provided here.
                      Given that, what might you think would be a modification to what was given earlier that would do what you now need?

                      Unrelated: Using the previous resulting text as the new starting point, you could always search for Login: and replace with nothing. :-)

                      Martin XM 1 Reply Last reply Reply Quote 0
                      • Martin XM
                        Martin X @Alan Kilborn
                        last edited by

                        @Alan-Kilborn

                        trust me i try to learn something
                        i spent today about 3 hours to find solution for various text files
                        but this last one was to difficult for me

                        Now I want to make it in one regex to save some space
                        So i want to use one regex command to delete second and third line and “Login:”
                        and leave only
                        xxxx

                        Can You help me one more time?

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

                          @Martin-X said in Regex plese help - how to remove every 2nd and 3rd:

                          So i want to use one regex command to delete second and third line and “Login:”

                          I will give you some guidance. If you look at @Alan-Kilborn regex you will see the line that is written back is enclosed in brackets in the Find What line. The replace with line is \1 which is how we write back that information within the bracket. By changing the contents of the bracket (essentially moving the bracket to the right) you will change what the regex keeps.
                          Also note that you may have overlooked the space in the line you want to keep, now removing the front portion, so keep that in mind when editing his regex.

                          Terry

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