gpt4 book ai didi

java - 提示如何从 List 中提取文本

转载 作者:行者123 更新时间:2023-11-30 11:00:27 25 4
gpt4 key购买 nike

在我的 java 应用程序中,我有一个 List<String> sbuff_Test = new ArrayList<>();我在执行期间填充的结构。当 sbuff_Test准备好了,我把它的每一串都放在一个 jTextArea 中.我的输出是这样的:

Please choose Node: 
2 : Low s23_t0
1 : High s23_t0 (Id = 0)

* TESTPAD MAIN MENU (v10r0p0) *
----------------------------------

LowPT PAD s23_t0 on node 2

1 = Initialize PAD
2 = PRODE MENU
3 = TTC MENU
4 = CM MENU
5 = FPGA MENU
6 = LINK MENU
7 = CAN MENU
8 = ELMB MENU
9 = SPLITTER MENU
10 = Change CURRENT PAD
11 = Reset full PAD
12 = Warm Initialize PAD
13 = Change Pad configuration
14 = Change CM latencies
15 = Phase measurement
16 = Power ON/OFF
17 = Print PAD Status
18 = Measurement loop
19 = Read CM trigger frequencies
20 = fast check of locks
21 = TRIGGER MENU
22 = Read CM BC ids
23 = Test CM BC ids with prode
24 = Test CM BC ids with TTC
25 = Test init low-high
0 = Quit
TESTPAD: ELMB MENU
(1) ELMB reset
(2) power OFF/ON ELMB on Node 1
(3) ELMB firm/hard version
(4) set CAN-debug ON/OFF
(5) set the communication rate
(6) download XPG file into FLASH for localInit
(0) exit
2
Firmware Version SV22
Hardware Version pad8
TESTPAD: ELMB MENU
(1) ELMB reset
(2) power OFF/ON ELMB on Node 1
(3) ELMB firm/hard version
(4) set CAN-debug ON/OFF
(5) set the communication rate
(6) download XPG file into FLASH for localInit
(0) exit

现在,我想知道如何只提取我需要的文本;例如,对于上面的文本:

LowPT  PAD  s23_t0  on node 2
Firmware Version SV22
Hardware Version pad8

问题是我必须删除的文本部分是可变的,我找不到解决这个问题的方法。你对类似的问题有什么建议?感谢提示。

最佳答案

编辑:

要删除不需要的短语,您只需要使用 matcher.replaceAll("") 方法,在此示例中,我将使用旧模式:

String text = jTextAreaName.getText();

//This is the list of the wanted groups
String[] patterns = new String[]{"(LowPT .+)[\\r\\n]", "(Firmware Version .+)[\\r\\n]", "(Hardware Version .+)[\\r\\n]"};


//Then delete the three matched groups like this
for(int i=0; i<patterns.length; i++) {
Pattern pattern = Pattern.compile(patterns[i]);
Matcher matcher = pattern.matcher(text);
while(matcher.find()) {
text = matcher.replaceAll("");
}
}

这是 Updated DEMO .


在这种情况下,您需要使用 Regex Matcher 匹配组只从中提取想要的部分:

String text = jTextAreaName.getText();

//This is the list of the wanted groups
String[] patterns = new String[]{"(LowPT .+)[\\r\\n]", "(Firmware Version .+)[\\r\\n]", "(Hardware Version .+)[\\r\\n]"};


//Then extract the three matched groups like this
String myResult="";
for(int i=0; i<patterns.length; i++) {
//compile each matching group and find matches.
Pattern pattern = Pattern.compile(patterns[i]);
Matcher matcher = pattern.matcher(text);
while(matcher.find()) {
myResult += matcher.group(1);
myResult += "\n";
}
}

这是一个 Live DEMO 你可以在哪里测试它,给出以下结果:

LowPT PAD s23_t0 on node 2

Firmware Version SV22

Hardware Version pad8

解释:

  • (LowPT .+)[\\r\\n] 是行 LowPT PAD s23_t0 on node 2 的匹配组.
  • (Firmware Version .+)[\\r\\n]Firmware Version SV22 行的匹配组。
  • (Hardware Version .+)[\\r\\n]Hardware Version pad8 行的匹配组。

关于java - 提示如何从 List<string> 中提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31534686/

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