mercredi 22 juillet 2020

Java regular expression Pattern for reading Multiple lines in a file [closed]

I have a log file with multiple lines with date/time at the beginning. something like this..

**2020-07-20 00:00:01 - Script ABC

XYZ

124**

2020-07-20 00:00:02 - Script DSS

PID

1212

Here I want to read every line that matches the date/time at the beginning and the next few lines till we find the next date/time.

I'm trying below

Scanner sc = new Scanner(new File("log_1.txt"));
Pattern regexp = Pattern.compile("^[1-9][0-9]{3}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\s*(.*)");
while (sc.hasNext())
    System.out.println(sc.next());

but it reads the whole log file at once and prints it. I want to read each line(s) with this pattern and do some manipulations etc.

Aucun commentaire:

Enregistrer un commentaire