I’m facing some trouble writing a regular expression in Java to parse information from a logfile.
I have a String where the structure "timeinstant: some strings with any character" is repeated from 1 to N times.
timeinstant has the format "dd/mm/yyyy hh:MM:ss:MMMMMM" (M being microseconds).
What I’m trying to do is to find the microseconds of last timeinstant contained in an incoming string.
For example, with the string
] 2012/04/02 16:28:51:861819: abcdefg : lwersdgsdg remote=xx.xxx.xx.xxx:yyy3f] accepted and identified as: John 2012/04/02 16:28:51:862987: pump: Received data on connection {John} [
I’d like m.find() to point to "987: pump...". In order to get this, I’m using a regex with lookahead:
"(\\d{3}:)(?!\\d{4}/\\d{2}/\\d{2}\\s\\d{2}:\\d{2}:\\d{2}:\\d{6})"
But right now m.find() is pointing to 819 (contained in 2012/04/02 16:28:51:861819).
Your regex is very near to the one you need.
In your negative lookhead, you just forgot that different timestamps are separated by several characters. So you have to add
.+or.*in your lookahead to specify that.Here is the regex you need:
In your example, it will give you the “987” you are looking for.