本文介绍了基于 Pattern Java 拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模式的日志文件-

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
....

我尝试使用 string.split(datepattern) 但它只给我字符串数组中的内容而不是日期.我也试过使用模式匹配器,但它只给我一个匹配日期的列表,而不是内容.

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

编辑-字符串模式="([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);

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

推荐答案

这将在每次找到逗号或 \n 换行符时拆分字符串:

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:

首先,我有一种感觉,您想先将整个日志文件加载到一个字符串中.如果您将按行处理它们(如果日志文件是 10GB 会发生什么?),这是一种很好的内存浪费.更好的方法是使用 BufferedReader 并每行执行一次.

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.

这篇关于基于 Pattern Java 拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:29
查看更多