Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse a log line in Java delimited by keywords?

I'm working on a log parser that should parse a line like this:

ID1 : 0     ID2 : 214 TYPE : ERROR      DATE : 2012-01-11 14:08:07.432 CLASS : Maintenance    SUBCLASS : Operations

ID1, ID2, TYPE, DATE, CLASS, and SUBCLASS are all keywords and I want to have something like this:

ID1 : 0  
ID2 : 214  
TYPE : ERROR  
DATE : 2012-01-11 14:08:07.432  
CLASS : Maintenance  
SUBCLASS : Operations

I am really quite new to regex and I have the following:

(ID1|ID2|TYPE|DATE|CLASS|SUBCLASS)\\s*:\\s*(.+?)\\s*[(ID1|ID2|TYPE|DATE|CLASS|SUBCLASS)]

Of course, it does not work.

Any advice will be very much appreciated.

like image 538
amor214 Avatar asked Feb 20 '26 19:02

amor214


1 Answers

The main problem in your expression are the square brackets, they create a character class, this matches exactly one character from those inside.

(ID1|ID2|TYPE|DATE|CLASS|SUBCLASS)\\s*:\\s*(.+?)\\s*[(ID1|ID2|TYPE|DATE|CLASS|SUBCLASS)]
                                                    ^                                  ^

I made the alternation at the end also a positive lookahead assertion (The group starting with ?=), so this is not matched, just ensured that one of those alternatives are ahead. I added also the end of the string $ to the alternation.

(ID1|ID2|TYPE|DATE|CLASS|SUBCLASS)\\s*:\\s*(.+?)\\s*(?=ID1|ID2|TYPE|DATE|CLASS|SUBCLASS|$)

See it here on Regexr, a good tool to test regular expressions!

like image 198
stema Avatar answered Feb 22 '26 07:02

stema