REGEX: Mark/Delete the 2 second line after some word



  • hello everyone. I need to mark/delete the 2 second line after some word. For exemple:

    1.line
    2.line
    3.bla bla bla WORD_1 bla bla
    4.line
    5.line
    6.line ETC

    So, I want to delete the line that contains WORD_1 and the next 2 lines (but without line 6). Can anyone help me?

    I try this regex, this will delete the line with WORD_1, but will not delete line 4 and 5. But will insert another line 4 instead delete.

    Search:
    ^.*WORD_1.*$
    Replace by:
    \n\n



  • Hello, Vasile,

    Not difficult, indeed !

    The general regex S/R, which deletes ANY line, containing a SPECIFIC word, with generic name = WORD, along with the N lines, following this line, is :

    SEARCH (?-s)^.*WORD.*\R(.*\R){N}

    REPLACE EMPTY

    So, in your case, the search regex should be (?-s)^.*WORD_1.*\R(.*\R){2}


    In the same way, the general regex S/R, below, which would delete ANY line, containing the word WORD and the N lines located BEFORE this line, is :

    SEARCH (?-s)(.*\R){N}.*WORD.*\R

    REPLACE EMPTY


    Notes :

    • Due to the (?-s) modifier, the dot, ., stands for a single standard character, only

    • As usual, the \R syntax matches any kind of Line Break ( \r\n in Windows files, \n in Unix files or \r in old Mac files )

    • The part .*\R represents a complete line, even empty, with its EOL character(s)

    • So the syntax (.*\R){N}, where N is an integer, stands N complete lines

    Best Regards

    guy038



  • well, guy38, nice present for me.

    thanks a lot !

    This is for you !

    https://www.youtube.com/watch?v=nPW5nfoIYf8


Log in to reply