• Login
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.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.
  • B
    bob_diebel
    last edited by Jul 2, 2021, 2:13 PM

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

    P 1 Reply Last reply Jul 2, 2021, 2:27 PM Reply Quote 0
    • P
      PeterJones @bob_diebel
      last edited by Jul 2, 2021, 2:27 PM

      @bob_diebel

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

      A 1 Reply Last reply Jul 2, 2021, 2:44 PM Reply Quote 0
      • A
        Alan Kilborn @PeterJones
        last edited by Jul 2, 2021, 2:44 PM

        @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. :-)

        P 1 Reply Last reply Jul 2, 2021, 2:51 PM Reply Quote 1
        • P
          PeterJones @Alan Kilborn
          last edited by Jul 2, 2021, 2:51 PM

          @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? 😉

          P 1 Reply Last reply Jul 2, 2021, 3:09 PM Reply Quote 0
          • P
            PeterJones @PeterJones
            last edited by Jul 2, 2021, 3:09 PM

            @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.
            B 1 Reply Last reply Jul 3, 2021, 11:43 PM Reply Quote 1
            • B
              bob_diebel @PeterJones
              last edited by Jul 3, 2021, 11:43 PM

              @PeterJones Thanx a bunch. Much appreciated

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