Looking for a string with regex and delete the whole line
13,854
Solution 1
Let's break it down:
^ # Start of line
.* # any number of characters (except newline)
[ \t] # whitespace (tab or space)
\# # hashmark
[ \t] # whitespace (tab or space)
.* # any number of characters (except newline)
or, in a single line: ^.*[ \t]#[ \t].*
Solution 2
try this
^(.*[#].*)$
or maybe
(?<=[\r\n^])(.*[#].*)(?=[\r\n$])
Related videos on Youtube
Author by
ChriS
Updated on June 04, 2022Comments
-
ChriS about 2 years
I am trying to find in Textpad a character with regex (for example "#") and if it is found the whole line should be deleted. The # is not at the beginnen of the line nor at the end but somewehre in between and not connected to another word, number or charakter - it stands alone with a whitespace left and right, but of course the rest of the line contains words and numbers.
Example:
My first line My second line with # hash My third line# with hash
Result:
My first line My third line# with hash
How could I accomplish that?
-
Tim Pietzcker over 10 yearsThis is essentially what my regex is doing, but with one problem:
\s
also matches a newline character, so the two linesa #\nbc
would be matched/removed as well. -
MadConan over 10 years@TimPietzcker: Fixed it.
-
ChriS over 10 yearsThat works fine thanks, but is is there a way that the lines are moved up? So far when I replace it with nothing Textpad keeps the empty line.
-
Tim Pietzcker over 10 yearsIn that case, you'll want to match the trailing
\r\n
as well. Just add(\r\n)?
to the end of the regex (made optional since the newline may be missing on the last line of the file).