gpt4 book ai didi

regex - PowerShell 正则表达式 : Capturing strings between two strings that is on multiple lines

转载 作者:行者123 更新时间:2023-12-02 23:07:24 25 4
gpt4 key购买 nike

我可能有这样的事情:

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 )但这给了我从第一个到最后一个第二个|B
这适用于 regex101.com,但不适用于我的 PowerShell ISE 应用程序,我猜这是因为我将风格设置为 PCRE(PHP)。

最佳答案

FIRST\|(?:(?!SECOND\|[^B])[\S\s])*?SECOND\|B
不会匹配 FIRST|与 SECOND|A(或任何非 B)相关联

https://regex101.com/r/e0CG9B/1

展开

 FIRST \| 
(?:
(?! SECOND \| [^B] )
[\S\s]
)*?
SECOND \| B

如果需要绝对内部 FIRST/SECOND 必须以不同的方式完成:
FIRST\|(?:(?!(?:FIRST|SECOND)\|)[\S\s])*SECOND\|B
https://regex101.com/r/qoT8U1/1

关于regex - PowerShell 正则表达式 : Capturing strings between two strings that is on multiple lines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59076290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com