【问题标题】:Match multiple lines usign Regex使用正则表达式匹配多行
【发布时间】:2017-10-03 09:58:28
【问题描述】:

我有以下文字。

^0001   HeadOne


@@
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been theindustry's standard dummy text ever since the 1500s, when an unknown printer took a galley of typeand scrambled it to make a type specimen book.

^0002   HeadTwo


@@
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been theindustry's standard dummy text ever since the 1500s, when an unknown printer took a galley of typeand scrambled it to make a type specimen book.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been theindustry's standard dummy text ever since the 1500s, when an unknown printer took a galley of typeand scrambled it to make a type specimen book.


^004    HeadFour


@@
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

^0004   HeadFour


@@
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been theindustry's standard dummy text ever since the 1500s, when an unknown printer took a galley of typeand scrambled it to make a type specimen book.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been theindustry's standard dummy text ever since the 1500s, when an unknown printer took a galley of typeand scrambled it to make a type specimen book.

下面是我用来查找的正则表达式。

@@([\n\r\s]*)(.*)([\n\r\s]+)\^

但这仅捕获^0001^0003,因为它们只有一个段落,但在我的文本中有多个段落内容。

我正在使用 VS 代码,有人可以告诉我如何在 VS 代码或 NPP 中使用 REGEX 捕获此类多参数字符串。

谢谢

【问题讨论】:

  • \s 通常是空格,我会尝试删除它或添加 * 来表示零个或多个
  • 预期的匹配是什么,为什么? @Calvin,在 VSCode 中,\s 不匹配换行符。如何定义尾随边界? @@ 总是在行首吗?
  • @WiktorStribiżew 使用@@时直接匹配
  • @WiktorStribiżew,我正在尝试匹配@@^ 之间的内容,让它成为任意数量的段落......
  • 我刚刚在VSCode中尝试了@@.*(?:[\s\r]?(?!\s*\^).*)*,它突出显示了@@之后的所有段落。尝试。我的 VSCode 版本是 1.16.1。要确保 @@ 仅在行首匹配,请在模式前添加 ^

标签: regex visual-studio-code find-replace


【解决方案1】:

关于 VSCode 正则表达式的一个奇怪的事情是 \s 不匹配所有换行符。需要使用[\s\r]来匹配所有这些。

记住这一点,您希望匹配以@@ 开头的所有子字符串,然后在行首或字符串末尾延伸到^

我建议:

@@.*(?:[\n\r]+(?!\s*\^).*)*

regex demo

注意:要仅匹配行首的 @@,请在模式的开头添加 ^^@@.*(?:[\s\r]+(?!\s*\^).*)*

注意 2:从VSCode 1.29 开始,您需要enable search.usePCRE2 option 才能在您的正则表达式模式中启用前瞻。

详情

  • ^ - 行首
  • @@ - 文字 @@
  • .* - 该行的其余部分(除换行符之外的 0+ 个字符)
  • (?:[\n\r]?(?!\s*\^).*)* - 0 次或多次连续出现:
    • [\n\r]+(?!\s*\^) - 一个或多个换行符后面没有 0+ 空格,然后是 ^ char
    • .* - 该行的其余部分

在 Notepad++ 中,使用 ^@@.*(?:\R(?!\h*\^).*)*,其中 \R 匹配换行符,\h* 匹配 0 个或多个水平空格(如果 ^ 始终是分隔符上的第一个字符,则删除行)。

【讨论】:

  • @user3872094:在记事本++中,使用^@@.*(?:\R(?!\h*\^).*)*
【解决方案2】:

我将您的输入数据插入 /tmp/test 并使用 perl 语法使以下内容工作

grep -Pzo "@@(?:\s*\n)+((?:.*\s*\n)+)(?:\^.*)*\n+" /tmp/test

这应该将不以 ^ 开头的段落放入 $1。您可能需要将 \r 添加回其中以使其完美匹配

【讨论】:

    猜你喜欢
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多