regex pattern
Hello,
I want to parse a log file that has the following structure using Java Regex pattern:
[1168921047] SERVICE ALERT: pool30;Image Processes;WARNING;SOFT;1;SNMP WARNING - *123*
[1168921065] SERVICE ALERT: pool30;Image Processes;OK;SOFT;1;SNMP OK-100
...
I am trying to use a multile pattern (with no success thus far), Could anyone have idea? thanks a lot
You don't say what is fixed and what you want to extract.
It would help if you'd describe how you want to parse it.
Sorry, I forgot to say it,
I want to parse this log file to get some info like. The structre of log file is fixed. Since I have tried using String split(";"), it still has problem. now i am looking for another way like regex pattern to deal with it.
- timestamp (1168921047)
- host name (pool30)
- service name (Image Processes)
- server status (WARNING or OK)
what have you got so far?
Thanks for replay, in more details: if met "WARNING" it will be saved into a list, if OK comes with the same host name (for example: pool30) should also be store into this list. the other line with "OK" I don't care about them.
a log file
[1168921025] SERVICE ALERT: pool23;Service1;OK;SOFT;1;SNMP OK-100
[1168921035] SERVICE ALERT: pool24;Service2;OK;SOFT;1;SNMP OK-100
[1168921047] SERVICE ALERT: pool30;Image Processes;WARNING;SOFT;1;SNMP WARNING - *123*
[1168921065] SERVICE ALERT: pool30;Image Processes;OK;SOFT;1;SNMP OK-100
[1168921047] SERVICE ALERT: pool32;Image Processes;WARNING;SOFT;1;SNMP WARNING - *103*
String[] tokens = log.split(";");
for(int i=1; i<tokens.length; i++){
// here I try to write a regex pattern
Pattern pattern = Pattern.compile("WARNING");
// match the keywords
Matcher matcher = pattern.matcher(tokens[i]);
if(matcher.matches()){
// save them into a list
}
>
I can't see where your code is going.You don't need to use split(). Just create a Pattern with groups that will capture the fields you want. Something likePattern p = Pattern.compile("\\[(\\d+)\\][^:]+:\\s+([^;]+);([^;]+);(WARNING|OK).*");
Thanks very much, that is I need,
I have tried your regex pattern in my application it list all the lines with status "OK" and "WARNING", my problem is how to list "WARNING" and "OK" (It should have the same host name of the line with "WARNING" like pool30). Is it a possible to write a condition in regex pattern or anything?
> Thanks very much, that is I need,
>
> I have tried your regex pattern in my application it
> list all the lines with status "OK" and "WARNING", my
> problem is how to list "WARNING" and "OK" (It should
> have the same host name of the line with "WARNING"
> like pool30). Is it a possible to write a condition
> in regex pattern or anything?
I don't understand! Have you listed out the contents of groups 1 to 4 when you get a match?
Yes, I mean it can list out the contents of group 1-4 when it matches. but how to list only the line with "OK" that fllows the line with "WARNING". Thanks!
[1168921025] SERVICE ALERT: pool23;Service1;OK;SOFT;1;SNMP OK-100 <-- this line I don't need
[1168921035] SERVICE ALERT: pool24;Service2;OK;SOFT;1;SNMP OK-100<-- this line I don't need
[1168921047] SERVICE ALERT: pool30;Image Processes;WARNING;SOFT;1;SNMP WARNING - *123* <-- that i need to list out
[1168921065] SERVICE ALERT: pool30;Image Processes;OK;SOFT;1;SNMP OK-100<-- this line also
[1168921047] SERVICE ALERT: pool32;Image Processes;WARNING;SOFT;1;SNMP WARNING - *103* <-- and this line
You have to do some of the work! You will need to build a map linking lines through the host name.
@sabre150thanks again for your help and hints.