gpt4 book ai didi

java - 按顺序匹配新行文本内容

转载 作者:行者123 更新时间:2023-12-01 13:46:38 26 4
gpt4 key购买 nike

我有以下由系统使用的输入:

-- file 1 (input) --
Test
One: any junk here
Two: any junk here
Three: any junk here

-

-- file 2 (input)
Test
One: any junk here
Three: any junk here

由于 file1 和 file2 重叠,我的 ONE_THREE 正则表达式(如下)始终在我的 ONE_TWO_THREE 匹配之前匹配 file1。

如何在正则表达式中更加具体,以便文件 1 仅与 ONE_TWO_THREE 匹配,文件 2 仅与 ONE_THREE 匹配(无需在 if 语句中移动正则表达式)?

// text string represents random input stream...in this case it's set to file1 contents for literary content to illustrate my parsing regex issue. It would be set set to file 2 per other incoming messages to the system.  Either way it represents an incoming file contents.                                                                                                                                                                                                              
String text = "Test\n"
+ "One: any junk here\nTwo: any junk here\nThree: any junk here";
Pattern ONE_THREE = Pattern.compile("^(One:\\s(.*)Three:\\s(.*))$", Pattern.MULTILINE | Pattern.DOTALL);
Pattern ONE_TWO_THREE = Pattern.compile("^(One:\\s(.*)Two:\\s(.*))$", Pattern.MULTILINE | Pattern.DOTALL);

Matcher m = null;
if ( ((m = ONE_THREE.matcher(text)).find()) ||
((m = ONE_TWO_THREE.matcher(text)).find()) ) {
// How can I make sure to only match ONE_TWO without moving it up in the if statement?
System.out.println("matched: " + m.pattern().toString()); // matches ONE_THREE
//
// delete everything but "Test" for any input
text = m.replaceAll("");
text = text .replaceAll("[\n]+$", ""); // delete any remaining /n
System.out.println(text);
}

注意:我正在尝试跟踪与正则表达式类型匹配的实例数量,因此我需要将它们分开。有没有办法通过 reg ex ONE_THREE 保证只有在其自己的行上的“One:”直接跟随下一行的“Three:”时才匹配(这样它就不会匹配 file1)?

最佳答案

一般情况下可以改成这样

 #  "^(One:\\s((?:(?!Two:|Three:).)*)Three:\\s(.*))$"

^
( # (1 start)
One: \s
( # (2 start)
(?:
(?! Two: | Three: )
.
)*
) # (2 end)
Three: \s
( .* ) # (3)
) # (1 end)
$

关于java - 按顺序匹配新行文本内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20319392/

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