Splitting a string based on Pattern Java
Solution 1
This will split the string each time a comma or a \n
newline is found:
String[] parsedString = logString.split("(,|\n)");
It should produce your desired output, but there are few potential problem I foresee here:
First I have a feeling you're trying to load the whole log file into a string first. This is a good waste of memory if you will be processing them by line (what happens if the log file is 10GB?). A better approach would be to use a BufferedReader and do them per line.
Secondly keep in mind a log output can have commas in itself, so above code will be buggy. Since the prefix part seem to be fixed-length, you might want to chop them using substring instead.
Solution 2
If you must use regex you could try it like this
Pattern p = Pattern.compile("(^[^,]*)(.*$)");
Matcher m = p.matcher(inputstring);
m.matches();
String part1 = m.group(1);
String part2 = m.group(2);
Then part1
should be everything up to the first comma, part2
the rest of the inputstring.
Using substring
would be easier though...
user3370268
Updated on June 05, 2022Comments
-
user3370268 almost 2 years
Hi I have log files of the following pattern-
2014-03-06 03:21:45,432 ERROR [mfs:pool-3-thread-19] dispatcher.StatusNotification - Error processing notification. Operation aborted. java.sql.SQLException: Network error IOException: Connection timed out: connect 2014-03-06 03:22:06,454 ERROR [mfs:pool-3-thread-19] dispatcher.ClientStatusNotification - Error processing notification. Operation aborted. java.sql.SQLException: Network error IOException: Connection timed out: connect 2014-03-06 03:22:27,462 ERROR [pool-1-thread-1] cluster.ClusterServiceImpl - unexpected error when trying to update LastCheckinTime java.sql.SQLException: Network error IOException: Connection timed out: connect ...
I am trying to split the string into substrings such that-
parsedString[0]=2014-03-06 03:21:45 parsedString[1]=,432 ERROR [mfs:pool-3-thread-19] dispatcher.StatusNotification - Error processing notification. Operation aborted. java.sql.SQLException: Network error IOException: Connection timed out: connect parsedString[2]=2014-03-06 03:22:06 ....
I tried using
string.split(datepattern)
but it only gives me the content in the string array and not the dates. I also tried using Pattern matcher but it only gives me a list of matching dates and not the content.How do I get both values into the same string array. Any help would be much appreciated. Thanks
Edit- String pattern="([0-9]{4}-[0-1][0-9]-[0-3][0-9]\s(?:[0-1][0-9]|[2][0-3]):[0-5][0-9]:[0-5][0-9],)"; String parsedLogMessage[]=GetLogString().split(pattern); this.MessageContent=Arrays.asList(parsedLogMessage);
This only gives the string split by regex and not the regex string itself
-
prasanth about 10 yearsBut this works for only one line.
Pattern.compile("(^[^,]*)(.*$)",Pattern.MULTILINE)
would do. -
piet.t about 10 yearsof course it has to go into a loop over all lines - either starting before
p.matcher
, then my solution would suffice, or beforem.matches
which would require the MULTILINE approach -
user3370268 about 10 yearsThanks for the lack of a more direct answer I am accepting this.And yes you are probably right loading the whole log file into a string is a waste of good memory.
-
tero17 about 6 yearsi have string which contains this char '|' i wanna split my string due to this special char is it possible with split ?