【发布时间】:2020-03-23 08:51:27
【问题描述】:
我可能有这样的事情:
FIRST|[some text here] (newline)
[insert text here] (newline)
SECOND|A (newline)
FIRST|[some text here] (newline)
[insert text here] (newline)
SECOND|B (newline)
FIRST|[some text here] (newline)
[insert text here] (newline)
SECOND|A (newline)
FIRST|[some text here] (newline)
[insert text here] (newline)
SECOND|B (newline)
FIRST|[some text here] (newline)
[insert text here] (newline)
SECOND|A (newline)
我只想捕获从FIRST 到SECOND|B 的所有内容,并排除从FIRST 到SECOND|A 的所有内容。
这篇文章中的顺序只是一个例子,可能与我正在使用的文件不同。括号中的文本可以是单词、数字、特殊字符等。(换行符)只是告诉你它在不同的行上。
我试过https://regex101.com/r/CwzCyz/2 (FIRST[\s\S]+SECOND\|B) 但这给了我从第一个 FIRST 到最后一个 SECOND|B
这适用于 regex101.com,但不适用于我的 PowerShell ISE 应用程序,我猜这是因为我将风格设置为 PCRE(PHP)。
【问题讨论】:
-
获取文件内容时使用
-Raw,然后使用(?s)FIRST.*?SECOND\|B -
这不太行,因为在遇到
SECOND|B之前,SECOND|A仍将作为匹配的一部分返回。 -
A和B真的是字母 A 和 B,还是它们代表其他东西。我们能找到SECOND|C或SECOND|Z或别的什么吗?
标签: regex powershell regex-group regex-greedy capturing-group