Delete an entire line if a specific word is found inside that line
-
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,
-
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 changexxx
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”. -
@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. -
@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?
-
@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. -
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.