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



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



  • @bob_diebel

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



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



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



  • @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.


  • @PeterJones Thanx a bunch. Much appreciated


Log in to reply