• Login
Community
  • Login

Delete an entire line if a specific word is found inside that line

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
6 Posts 3 Posters 34.4k Views
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.
  • C
    Costin R
    last edited by Mar 26, 2021, 7:30 AM

    Hi,

    Is it possible to build a regex to find/mark an entire line of text when a specific word (specifically “#EXTINF”) is found in that line, so to be able to replace that line with “” (nothing) i.e. in order to remove that line?

    I have a text with about 6.000 lines, and I need to remove a lot of them, the lines containing that set of characters.

    Thank you,

    1 Reply Last reply Reply Quote 0
    • A
      AdrianHHH
      last edited by Mar 26, 2021, 9:16 AM

      I see two possibilities depending on exactly what is wanted.

      To remove the lines entirely, use the MARK tab from the search/replace dialogue (click Control+Alt+M), select “Bookmark line”, enter the seach string and click “Mark all”. Expect to see blue blobs at the left of the marked lines. Next use Menu => Search => Bookmark => Remove bookmarked lines.

      The question also says to replace the line with nothing which might mean changing those lines to be empty lines but not deleting them. For this you can use a replace command, select “Regular expression” and “Wrap around”, in the “Find what” enter ^.*xxx.*$ but change xxx to the string you want. Be aware that any meta chararacters used in regular expressions must be escaped. Leave the “Replace with” empty and click on “Replace all”.

      A 1 Reply Last reply Mar 26, 2021, 11:43 AM Reply Quote 3
      • A
        Alan Kilborn @AdrianHHH
        last edited by Mar 26, 2021, 11:43 AM

        @AdrianHHH 's regular expression is not totally safe and can match more than intended in certain circumstances (i.e., lines that do not contain xxx). I would change it by putting an (?-s) at the start.

        A 1 Reply Last reply Mar 27, 2021, 9:16 AM Reply Quote 1
        • A
          AdrianHHH @Alan Kilborn
          last edited by Mar 27, 2021, 9:16 AM

          @Alan-Kilborn You have me worried. I have used that sort of expression many times without issue. Could you please explain how or when it can match unwanted line?

          A 1 Reply Last reply Mar 27, 2021, 10:59 AM Reply Quote 0
          • A
            Alan Kilborn @AdrianHHH
            last edited by Mar 27, 2021, 10:59 AM

            @AdrianHHH said in Delete an entire line if a specific word is found inside that line:

            Could you please explain how or when it can match unwanted line?

            Oh, sorry. I should have done that.
            As your expression used the . character, it could be affected by the . matches newline checkbox. Since you didn’t specify the state along with your regex, it was an incomplete specification.

            If you add (?-s) at the start of your regex, it removes the need to specify that the . matches newline checkbox should be unticked.

            Similarly, if you add (?s) at the start of your regex, it removes the need to specify that the . matches newline checkbox should be ticked.

            Start with this data:

            abc
            xxx
            abc
            xxx
            

            First, try (?-s)^.*xxx.*$ and see that it works as you intend (for OP’s need).

            Next, try (?s)^.*xxx.*$ and see that it matches more than intended.

            All I was trying to say was that if OP accidentally had the . matches newline box ticked from some prior operation, your regex would match unintended data. I could have said it better the first time around, sorry.

            1 Reply Last reply Reply Quote 2
            • A
              AdrianHHH
              last edited by Apr 2, 2021, 8:24 AM

              Thank you @Alan-Kilborn for that explanation. I know and use the dot-matches-newline, and yes I should have specified it in my answer.

              1 Reply Last reply Reply Quote 1
              5 out of 6
              • First post
                5/6
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors