Community
    • Login

    A RegEx question - How to remove a line, including the \r\n? Thanx,

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 3 Posters 9.3k 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.
    • bob_diebelB
      bob_diebel
      last edited by

      A RegEx question - How to remove a line, including the \r\n? Thanx,

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

        @bob_diebel

        regular expression mode: FIND ^.*\r\n REPLACE empty.

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

          @PeterJones said in A RegEx question - How to remove a line, including the \r\n? Thanx,:

          ^.*\r\n

          I would adjust that to be (?-s)^.*\r\n for maximum safety. :-)

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

            @Alan-Kilborn said in A RegEx question - How to remove a line, including the \r\n? Thanx,:

            for maximum safety.

            “a line”, “all the lines”, what’s the difference? 😉

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

              @PeterJones said in A RegEx question - How to remove a line, including the \r\n? Thanx,:

              “a line”, “all the lines”, what’s the difference? 😉

              @bob_diebel ,
              In case you didn’t know, the (?-s) that @Alan-Kilborn added makes sure that the regex ignores the state of . matches newline, and instead forces the regex to have . not match newline. Mine would have worked if . matches newline is off, but will match from the beginning to end of the file if . matches newline was on. We regular Forum contributors try (but often fail) to use (?-s) or (?s) so that our answers will work no matter what the setting of . matches newline. But since the .* is rather permissive anyway, a REPLACE ALL would have deleted the whole file no matter what the setting of . matches newline.

              Some more caveats: the regex will match any line with zero or more characters followed by the CRLF newline sequence.

              • My guess is that you really wanted to match some specific line. You will need a regular expression between the ^ (start of line) and \r\n (CRLF) that matches only the line(s) that you want to delete: (?-s)^☒\r\n, where I use ☒ as a placeholder for your regular expression to match the particular line(s).
              • If one of the lines you want to delete might be at the end of the file and not have a newline, you might need to use something like
                • (?-s)^☒(\r\n|\z) – the parentheses make a group, and the | says “OR”, and the \z says “end of file”. (same ☒ notation). But this still has the “feature” that it will leave a final newline (so you might consider it an “empty line”) when the last matched line is the last line with no newline.
                • If you want to also delete the last newline, you could use (?-s)^☒\r\n|\r\n☒\z – which says to grab the whole line including CRLF if there’s a CRLF afterward, or grab the previous newline through the end-of-file if the line doesn’t end in CRLF. This will make sure there is no “empty line” at the end of the file.
              • Another option is to use the Search > Mark dialog with teh “bookmark line” option checked, to mark all the lines; then use Search > Bookmark > Remove Bookmarked Lines to delete the lines.
              bob_diebelB 1 Reply Last reply Reply Quote 1
              • bob_diebelB
                bob_diebel @PeterJones
                last edited by

                @PeterJones Thanx a bunch. Much appreciated

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