I’m writing a Java program where I have to extract some data from a chat log file for a further processing using regex(I am new to regular expressions by the way). The chat log schema is defined as follows:[hh:mm:ss] string.
But the specific lines I would like to extract data are in the form of
[hh:mm:ss] <data1> data2. The data I would like to extract are hh:mm:ss, data1 and data2.
At first, I have tried to extract the time which was easier using
Pattern.compile(“(\d{2}:\d{2}:\d{2}).
I have even been able to extract the data1 separately using
Pattern p1=Pattern.compile(“<(.*)>”); and it was fine.
But when I try to get “hh:mm:ss”,data1 and data2 by using the following regex
Pattern p=Pattern.compile(“(\d{2}:\d{2}:\d{2}) <(.*)> (.*)”) I have no match found.
So does any one have an Idea on how I can proceed in that case to achieve my goal?
I’m writing a Java program where I have to extract some data from a
Share
Well if you were matching your own pattern everything would have been fine. You forget about the brackets of the time: [ hh:mm:ss ] . See here:
This produces:
So first string was matched and second one – not. Just as expected.
Probably you will just need to change your pattern to
\\[(\\d{2}:\\d{2}:\\d{2})\\] <(.*)> (.*).