How to delete all lines in a document which has a specific text



  • How to delete all lines in a document which has a specific text ; lets say : (MEX)



  • Hello, D. Visser,

    I just think about TWO ways to achieve it :

    • A) With bookmarks

    • B) With regexes


    A) :

    • Open the Mark dialog ( Search > Mark… )

    • Type your Your Character|String|Range of Words|Regex expression, in the Find what zone

    • Check the Bookmark line option

    • Possibly, check the Purge for each search option ( in order to be sure that the bookmarked lines concern the present search, only ! )

    • Check/Uncheck the Match whole word only, Match case and Wrap around options, if preferred

    • For regexes, select the Regular expression search mode

    • Click on the Mark All button

    • Close the Mark dialog ( ESC )

    • Select the menu option Search > Bookmark > Remove Bookmarked Lines

    B)

    • Open the Replace dialog ( Ctrl + H )

    • Type (?-s)^.*Your Character|String|Range of Words|Regex expression.*\R, in the Find what zone

    • Let the Replace with zone EMPTY

    • Check/Uncheck the Match whole word only, Match case and Wrap around options, if preferred

    • For a regex expression, select the Regular expression search mode ( IMPORTANT )

    • Click on the Replace All button

    Et voilà !


    NOTES :

    • First, the in-line modifier (?-s) ensures you that the dot meta-character will match standard characters, only, even if you previously checked the . matches newline option !

    • Then, the regex engine looks, from the beginning of a line (^ ), for any amount, even empty, of characters (.* ), followed by what you’re searching, then followed by any range, even empty, of characters (.* ), till the End of Line character(s) (\R )

    • Due to the EMPTY Replacement field , this entire line, containing your searched expression, is, therefore, deleted !

    Best Regards,

    guy038



  • @guy038 said:

    (?s)^.*

    Thank You guy038 for yr help.
    Test however not yet successful.
    Ad B replace info:
    Please give me the full search string to remove any line which contains the word (MEX)

    David



  • Problem solved guy038 via the bookmark method

    Thank You

    David



  • Hi, D visser,

    Oh ! you’re right ! Although it was correctly written in the Notes section, the general regex should be :

    (?-s)^.*Your Character|String|Range of Words|Regex expression.*\R ( and NOT (?s)..... ! )

    So, I updated my previous post !


    Below, here are the FOUR search regex, to enter in the Find what zone, in order to delete :

    • A) All lines containing the string MEX, in that EXACT case

    (?-is)^.*MEX.*\R

    • B) All lines containing the string MEX, WHATEVER its case

    (?i-s)^.*MEX.*\R

    • C) All lines containing the word MEX, in that EXACT case

    (?-is)^.*\bMEX\b.*\R

    • D) All lines containing the word MEX, WHATEVER its case

    (?i-s)^.*\bMEX\b.*\R


    Notes :

    • Remember that the Replace with: zone must be EMPTY !

    • The \b assertions represents, either, the zero-length limit, between :

      • ( A non-word character OR the very beginning of a file ) AND a word character

      • A word character AND ( a non-word character OR the very end of a file )

    • The in-line modifier (?-i) forces the search, in a NON-insensitive way

    • The in-line modifier (?i) forces the search, in a insensitive way

    So, suppose the original text, of four lines :

    Line 1 : Example ofMEX text
    Line 2 : Example ofmeX text
    Line 3 : Example of MEX text
    Line 4 : Example of MEx text
    

    Then :

    • The regex A) would delete the lines 1 and 3

    • The regex B) would delete the lines 1, 2, 3 and 4

    • The regex C) would delete the line 3

    • The regex D) would delete the lines 3 and 4

    Cheers,

    guy038


Log in to reply