1

I am trying to extract the end to end delay value which is exist in each line in a text file. I used regular expression to get only the number at the end of each line, but i got error for the regular expression which is:

illegal escape character

moreover, i need to open the file to take each line and extract the end-end delay value and store it on another text file (all the extracted end-end delay).

Here is my code:

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import javax.swing.filechooser.FileSystemView;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author DevAdmin
 */
public class extarctor {

    public static final String s_example = "sender id: 116/sequence number: 117/depth: 443/sending time: 4/23/2020 2:08:54 AM/data: Hello I am SN: 116 this is event # 117 from my sideEnd-End Delay is:2.74550137092987E-05delay registered @ Sink: 621.932901880787";

    public static void main(String[] args) throws IOException {
        //text file, should be opening in default text editor
        File file = new File(FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath()+ "/res/End-End-Delay.txt");

        //first check if Desktop is supported by Platform or not
        if(!Desktop.isDesktopSupported()){
            System.out.println("Desktop is not supported");
            return;
        }

        Desktop desktop = Desktop.getDesktop();
        if(file.exists()) System.out.println("Good, keep going!");

        System.out.println(s_example.matches("\d+\.\d+$"));



    }


}

1 Answers1

1

Use Matcher::group to extract the required number.

Demo:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String s_example = "sender id: 116/sequence number: 117/depth: 443/sending time: 4/23/2020 2:08:54 AM/data: Hello I am SN: 116 this is event # 117 from my sideEnd-End Delay is:2.74550137092987E-05delay registered @ Sink: 621.932901880787";

        Pattern pattern = Pattern.compile("\\d+.\\d+$");
        Matcher matcher = pattern.matcher(s_example);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

Output:

621.932901880787
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Working, but then how to parse all the lines in the text file and store all the extracted values on another text file ?? – LittleCoder May 08 '20 at 00:43
  • You need a loop to read the lines from the file and extract number from each of them. You can store the numbers into an ArrayList and finally write into the other file. Feel free to comment in case of any further doubt. – Arvind Kumar Avinash May 08 '20 at 00:58