Search code examples
notepad++

Replace or remove everything after the first few lines


I've got a lot of long XML files and I want to replace everything in them after the 4th line, I've been looking around and can't find anything with the expressions to select everything after the 4th line.

<?xml version="1.0" encoding="UTF-8"?>
<CMapData>
<name>garage_door_locked</name>
<parent />
select everything from here to the end of the file

Is such a thing possible with regular expressions? Also, the first 4 lines are entirely different in each of my files. I just need to skip them somehow.


Solution

    • Ctrl+H
    • Find what: \A(?:.*\R){4}\K[\s\S]+\z
    • Replace with: LEAVE EMPTY
    • SELECT Regular expression
    • UNTICK . matches newline
    • Replace all

    Explanation:

    \A          # beginning of file
        (?:         # non capture group
            .*          # 0 or more any character but newline
            \R          # any kind of linebreak (i.e. \r, \n, \r\n)
        ){4}        # end group, must appear 4 times
        \K          # forget all we have seen until this position
        [\s\S]+     # 1 or more any character
    \z          # end of file
    

    Screenshot (before):

    enter image description here

    Screenshot (after):

    enter image description here