基于Java模式分割字符串

2024-05-27

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

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

我正在尝试将字符串拆分为子字符串,以便-

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)但它只给我字符串数组中的内容,而不是日期。 我也尝试使用模式匹配器,但它只给我一个匹配日期的列表,而不是内容。

如何将两个值放入同一个字符串数组中。 任何帮助将非常感激。 谢谢

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

这只给出了由正则表达式分割的字符串,而不是正则表达式字符串本身


如果您必须使用正则表达式,您可以这样尝试

Pattern p = Pattern.compile("(^[^,]*)(.*$)");
Matcher m = p.matcher(inputstring);
m.matches();
String part1 = m.group(1);
String part2 = m.group(2);

Then part1应该是第一个逗号之前的所有内容,part2输入字符串的其余部分。

Using substring不过会更容易...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于Java模式分割字符串 的相关文章

随机推荐