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

^(.*[#].*)$

Regular expression visualization

Debuggex Demo

or maybe

(?<=[\r\n^])(.*[#].*)(?=[\r\n$])

Regular expression visualization

Debuggex Demo

Share:
13,854

Related videos on Youtube

ChriS
Author by

ChriS

Updated on June 04, 2022

Comments

  • ChriS
    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
    Tim Pietzcker over 10 years
    This is essentially what my regex is doing, but with one problem: \s also matches a newline character, so the two lines a #\nbc would be matched/removed as well.
  • MadConan
    MadConan over 10 years
    @TimPietzcker: Fixed it.
  • ChriS
    ChriS over 10 years
    That 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
    Tim Pietzcker over 10 years
    In 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).