首页 > 解决方案 > 使用 Notepad++ 提取基于单行文本的特定文本行

问题描述

我在一个巨大的文本文件中有下面的行,并且想要提取 LineD 和它前面的其他行。如果该集合不包含 D 行,则应忽略该集合

LineA
LineB 
LineC

Line1
Line2 
Line3
LineD

Linex
LineY 
LineZ

Line4
Line5 
Line6
LineD

输出应该是

Line1
Line2 
Line3
LineD

Line4
Line5 
Line6
LineD

标签: notepad++

解决方案


  • Ctrl+H
  • 找什么:(?:\A|\R\R)(?:(?!LineD)[\s\S])*?(?=\R\R|\z)
  • 用。。。来代替:LEAVE EMPTY
  • 检查 火柴盒
  • 检查 环绕
  • CHECK 正则表达式
  • 取消选中 . matches newline
  • Replace all

解释:

(?:\A|\R\R)     # non capture group, beginning of file OR 2 linebreaks
(?:             # non capture group
  (?!LineD)     # negative lookahead, make sure sure we haven't "LineD"
  [\s\S]        # any character, including linebreak
)*?             # end group, may appear 0 or more times, not greedy
(?=\R\R|\z)     # positive lookahead, make sure we have a double linebreak OR end of file after.

截图(之前):

在此处输入图像描述

截图(之后):

在此处输入图像描述


推荐阅读