Local Hero
Local Hero

Reputation: 503

Hint on how extract a text from a List<string>

In my java application I have a List<String> sbuff_Test = new ArrayList<>(); structure that I fill during the execution. When the sbuff_Test is ready, I put every string of it in a jTextArea. My output is something like that:

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

Now, I want an hint on how extract only the text that I need; for example, for the text above:

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

The trouble is that the part of text that I must delete is variable and I can't find an approach for this problem. What do you suggest for a similar problem? Thanks for the hint.

Upvotes: 2

Views: 93

Answers (2)

cнŝdk
cнŝdk

Reputation: 32145

EDIT:

To delete the unwanted phrase you just need to use matcher.replaceAll("") method, In this Example I will use the old patterns:

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("");
    }
}

Here's the Updated DEMO.


In that case you need to use a Regex Matcher and matching groups to only extract the wanted parts from it:

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";
     }
}

This a Live DEMO where you can test it, giving the following result:

LowPT PAD s23_t0 on node 2

Firmware Version SV22

Hardware Version pad8

Explanation:

  • (LowPT .+)[\\r\\n] is a matching group for the line LowPT PAD s23_t0 on node 2.
  • (Firmware Version .+)[\\r\\n] is a matching group for the line Firmware Version SV22.
  • (Hardware Version .+)[\\r\\n] is a matching group for the line Hardware Version pad8.

Upvotes: 1

Pol
Pol

Reputation: 27

If you only need LowPT,Firmware and Hardware Version ,read your file line by line. If your line contains one of the above keywords print the current line and continue to the next one.

Upvotes: 0

Related Questions