Community
    • Login

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

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 2 Posters 32.9k Views 1 Watching
    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.
    • D VisserD Offline
      D Visser
      last edited by

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

      1 Reply Last reply Reply Quote 0
      • guy038G Online
        guy038
        last edited by guy038

        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

        1 Reply Last reply Reply Quote 0
        • D VisserD Offline
          D Visser
          last edited by

          @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

          1 Reply Last reply Reply Quote 0
          • D VisserD Offline
            D Visser
            last edited by

            Problem solved guy038 via the bookmark method

            Thank You

            David

            1 Reply Last reply Reply Quote 0
            • guy038G Online
              guy038
              last edited by guy038

              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

              1 Reply Last reply Reply Quote 0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • First post
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors