简体   繁体   English

正则表达式查找与特定模式匹配的行

[英]Regex to find lines that match a specific pattern

I have a txt file and it has the data has a /////////// to divide the results.我有一个 txt 文件,它的数据有一个///////////来划分结果。 I need to find the results that only have 1 line.我需要找到只有 1 行的结果。

I've tried but I can't seem to figure out how to differentiate the multiple lines between the ///////////我已经尝试过,但我似乎无法弄清楚如何区分///////////之间的多行

For example, I just need to find the a and the d.例如,我只需要找到 a 和 d。

/////////// 
a
///////////
b
c
//////////
d
/////////

This will pull the letters you need into a capturing group:这会将您需要的字母拉入捕获组:

\/+\s+(\S+)\s+(?=\/+)

Explanation:解释:

  • At least one / , followed by至少一个/ ,后跟
  • At least one whitespace \s (includes newlines), followed by至少一个空格\s (包括换行符),后跟
  • At least one non-whitespace character \S , followed by至少一个非空白字符\S ,后跟
  • At least one whitespace \s , and至少一个空格\s
  • At least one / , in a positive lookahead group至少一个/ ,在一个积极的前瞻组中

Demo: https://regex101.com/r/pHg9ov/3演示: https://regex101.com/r/pHg9ov/3

  • Ctrl + F Ctrl + F
  • Find what: ^/+\R\K.+(?=\R/+$)查找内容: ^/+\R\K.+(?=\R/+$)
  • CHECK Wrap around检查环绕
  • CHECK Regular expression CHECK正则表达式
  • UNCHECK . matches newline取消选中. matches newline . matches newline
  • Find All in Current Document在当前文档中查找全部

Explanation:解释:

^           # beginning of line
  /+        # 1 or more slashes
  \R        # any kind of linebreak
\K          # reset match
.+          # 1 or more any character but newline
(?=         # positive look ahead, make sure we have after:
  \R          # any kind of linebreak
  /+          # 1 or more slashes
  $           # end of ine
)           # end look ahead

Screenshot (before):截图(之前):

在此处输入图像描述

Screenshot (after):截图(之后):

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM