Regex 查找没有指定字符串的行并删除空行

Regex 查找没有指定字符串的行并删除空行,regex,Regex,因此,我知道如何查找不包含特定字符串的所有行。但当我使用它时,它会留下很多空换行符,例如,在文本编辑器替换(Notepad++、Sublime等)中 是否有一种方法也可以删除同一正则表达式中替换所留下的空行,或者,正如公认答案中所提到的,“这不是正则表达式……应该做的事情” 示例,基于该问题的示例: 输入: 期望输出: [编辑-1] 让我们再试一次:我想要的是一种使用regex replace删除文本编辑器中不包含hede的所有内容的方法。如果我尝试*hede.*它将找到所有hede: 但它不

因此,我知道如何查找不包含特定字符串的所有行。但当我使用它时,它会留下很多空换行符,例如,在文本编辑器替换(Notepad++、Sublime等)中

是否有一种方法也可以删除同一正则表达式中替换所留下的空行,或者,正如公认答案中所提到的,“这不是正则表达式……应该做的事情”

示例,基于该问题的示例:

输入: 期望输出: [编辑-1] 让我们再试一次:我想要的是一种使用regex replace删除文本编辑器中不包含
hede
的所有内容的方法。如果我尝试
*hede.*
它将找到所有
hede

但它不会移除。对于一个短文件,这很容易手动完成,但是这里的想法是在一个更大的文件中替换,超过1000行,但其中包含20-50行所需的字符串

如果我使用
^((?!hede.)*$
并将其替换为零,那么我将以空行结束:

对于比我更了解regex的人来说,我认为这是一个简单的问题:一个regex替换也能删除那些留下的空行吗?

您尝试过:

.*hede.*
我不知道你为什么要反向搜索这个

您可以像以下那样使用
sed

sed -e '/.*hede.*/!d' input.txt
您是否尝试过:

.*hede.*
我不知道你为什么要反向搜索这个

您可以像以下那样使用
sed

sed -e '/.*hede.*/!d' input.txt
使用记事本++

  • Ctrl+H
  • 查找内容:
    ^((?!hede)。)*(?:\R |\z)
  • 替换为:
    留空
  • 检查匹配案例
  • 检查环绕
  • 检查正则表达式
  • 取消选中
    。匹配换行符
  • 全部替换
说明:

^                   # beginning of line
  ((?!hede).)*      # tempered greedy token, make sure we haven't hede in the line
  (?:\R|\z)         # non capture group, any kind of line break OR end of file
 ^        # start of a line
 (?!)     # a Negative Lookahead 
 .         # matches any character (except for line terminators)
 *        # matches the previous token between zero and unlimited times,
 hede   # matches the characters hede literally
 \s       # matches any whitespace character (equivalent to [\r\n\t\f\v ])
 ?         # matches the previous token between zero and one times,
屏幕截图(之前):

^                   # beginning of line
  ((?!hede).)*      # tempered greedy token, make sure we haven't hede in the line
  (?:\R|\z)         # non capture group, any kind of line break OR end of file
 ^        # start of a line
 (?!)     # a Negative Lookahead 
 .         # matches any character (except for line terminators)
 *        # matches the previous token between zero and unlimited times,
 hede   # matches the characters hede literally
 \s       # matches any whitespace character (equivalent to [\r\n\t\f\v ])
 ?         # matches the previous token between zero and one times,

屏幕截图(之后):

^                   # beginning of line
  ((?!hede).)*      # tempered greedy token, make sure we haven't hede in the line
  (?:\R|\z)         # non capture group, any kind of line break OR end of file
 ^        # start of a line
 (?!)     # a Negative Lookahead 
 .         # matches any character (except for line terminators)
 *        # matches the previous token between zero and unlimited times,
 hede   # matches the characters hede literally
 \s       # matches any whitespace character (equivalent to [\r\n\t\f\v ])
 ?         # matches the previous token between zero and one times,
使用记事本++

  • Ctrl+H
  • 查找内容:
    ^((?!hede)。)*(?:\R |\z)
  • 替换为:
    留空
  • 检查匹配案例
  • 检查环绕
  • 检查正则表达式
  • 取消选中
    。匹配换行符
  • 全部替换
说明:

^                   # beginning of line
  ((?!hede).)*      # tempered greedy token, make sure we haven't hede in the line
  (?:\R|\z)         # non capture group, any kind of line break OR end of file
 ^        # start of a line
 (?!)     # a Negative Lookahead 
 .         # matches any character (except for line terminators)
 *        # matches the previous token between zero and unlimited times,
 hede   # matches the characters hede literally
 \s       # matches any whitespace character (equivalent to [\r\n\t\f\v ])
 ?         # matches the previous token between zero and one times,
屏幕截图(之前):

^                   # beginning of line
  ((?!hede).)*      # tempered greedy token, make sure we haven't hede in the line
  (?:\R|\z)         # non capture group, any kind of line break OR end of file
 ^        # start of a line
 (?!)     # a Negative Lookahead 
 .         # matches any character (except for line terminators)
 *        # matches the previous token between zero and unlimited times,
 hede   # matches the characters hede literally
 \s       # matches any whitespace character (equivalent to [\r\n\t\f\v ])
 ?         # matches the previous token between zero and one times,

屏幕截图(之后):

^                   # beginning of line
  ((?!hede).)*      # tempered greedy token, make sure we haven't hede in the line
  (?:\R|\z)         # non capture group, any kind of line break OR end of file
 ^        # start of a line
 (?!)     # a Negative Lookahead 
 .         # matches any character (except for line terminators)
 *        # matches the previous token between zero and unlimited times,
 hede   # matches the characters hede literally
 \s       # matches any whitespace character (equivalent to [\r\n\t\f\v ])
 ?         # matches the previous token between zero and one times,

另一种尝试
查找内容:
^(?。*hede.*\s?

替换为:
nothing

说明:

^                   # beginning of line
  ((?!hede).)*      # tempered greedy token, make sure we haven't hede in the line
  (?:\R|\z)         # non capture group, any kind of line break OR end of file
 ^        # start of a line
 (?!)     # a Negative Lookahead 
 .         # matches any character (except for line terminators)
 *        # matches the previous token between zero and unlimited times,
 hede   # matches the characters hede literally
 \s       # matches any whitespace character (equivalent to [\r\n\t\f\v ])
 ?         # matches the previous token between zero and one times,
另一种尝试
查找内容:
^(?。*hede.*\s?

替换为:
nothing

说明:

^                   # beginning of line
  ((?!hede).)*      # tempered greedy token, make sure we haven't hede in the line
  (?:\R|\z)         # non capture group, any kind of line break OR end of file
 ^        # start of a line
 (?!)     # a Negative Lookahead 
 .         # matches any character (except for line terminators)
 *        # matches the previous token between zero and unlimited times,
 hede   # matches the characters hede literally
 \s       # matches any whitespace character (equivalent to [\r\n\t\f\v ])
 ?         # matches the previous token between zero and one times,

我有。这里的想法是删除文本编辑器中不包含“hede”的所有内容-这样做会找到“hede”,是的,但不会删除所有不包含“hede”的内容。我在这个问题上解释得太难了吗?我可以使用
sed
,就像我可以使用
getcontentsomefile.txt{$\u-匹配“hede”}
,但这不是我的想法……我有。这里的想法是删除文本编辑器中不包含“hede”的所有内容-这样做会找到“hede”,是的,但不会删除所有不包含“hede”的内容。我在这个问题上解释得这么难吗?我可以使用
sed
,就像我可以使用
get content somefile.txt{$\uu-match“hede”}
,但这不是这里的想法……我认为文本编辑器不是适合这些工作的工具。使用sed.you可以,我也同意——我可以使用powershell、bash和其他CLI来实现这一点,但这里的想法是了解这是否可以在文本编辑器上实现。我的最爱是崇高的,但我也喜欢记事本++,托托实际上可以想出一个答案。就我个人而言,我喜欢把它看作是一种扩展知识、超越自我的方式,你知道吗?我已经知道如何使用CLI,但不知道它是否可以通过文本编辑器实现。
^(?。*hede.*\s?
替换为
你应该把它作为一个答案,@HajiRahmatullah!!工作起来很有魅力@Joao Ciocca,,它已经有了一个公认的答案,只是尝试分享其他选择,我认为文本编辑器不是这些工作的合适工具。使用sed.you可以,我也同意——我可以使用powershell、bash和其他CLI来实现这一点,但这里的想法是了解这是否可以在文本编辑器上实现。我的最爱是崇高的,但我也喜欢记事本++,托托实际上可以想出一个答案。就我个人而言,我喜欢把它看作是一种扩展知识、超越自我的方式,你知道吗?我已经知道如何使用CLI,但不知道它是否可以通过文本编辑器实现。
^(?。*hede.*\s?
替换为
你应该把它作为一个答案,@HajiRahmatullah!!就像一个魔咒@Joao Ciocca,,,它已经有了一个公认的答案,只是尝试分享其他选择,,,我已经有一段时间没有从Notepad++转到Sublime了,但这一点实际上可能会让我回到它,你知道吗?谢谢你!我已经有一段时间没有从记事本++转到Supreme了,但这一点可能会让我回到它,你知道吗?谢谢你!