Regular expression never matches
I have the following entries in my log files:
09-22-11 12:35:09 1ms INFO ...erChangeSetListener:91 11 processing changeSet for class:4328,at version:1316720109100
09-22-11 12:35:09 779ms INFO ...erChangeSetListener:91 11 processing changeSet for class:4334,at version:1316720109882
09-22-11 12:35:09 1ms INFO ...erChangeSetListener:91 11 processing changeSet for class:4328,at version:1316720109882
09-22-11 12:35:11 1s WARNING QueryServiceImpl:100 - no existing index for class:4328
09-22-11 12:35:11 SEVERE QueryRequest:107 7 Aod query resulted in error:No index available for class:4328
09-22-11 12:35:11 SEVERE AuthenticationTask:48 - EndUserException: an error occurred when p开发者_如何学Crocessing the query Dump: /data1/amir/dev/devots/logs/dumps/22i123511.dump
that I'm applying this regexp to :
final String pattern = "^(\\d{2}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})" //date
+ "[.]*" //ignore for now
+ "(SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST)" //severity
+ "[.]*"; //ignore the rest for now
final Pattern regex = Pattern.compile(pattern);
final Matcher m = regex.matcher(currentLine);
if (m.matches()) {
for (int i = 1; i <= m.groupCount(); i++) {
System.out.format("[%d] \"%s\"%n", i, m.group(i));
}
}
But my matches never returns true. I don't understand why. I'd like to resiliently handle these different types of entries. Notice the last log entry has a dump file associated with it.
The problem is here:
+ "[.]*" //ignore for now
The dot in "[.]*"
only matches a literal dot, it does not have its usual meaning of "any non-line-separator character." You need ".*"
You can't do [.]*
. .*
will suffice. You want any character, not a dot.
Test your regex here: http://regexpal.com/
Start with a very simple match and start adding complexity bit by bit.
Also, for clarity, consider naming your groups like:
^(?<date>\d{2}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).*(?<type>SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST).*
精彩评论