【问题标题】:split a log text using a regex, ignoring first match使用正则表达式拆分日志文本,忽略第一个匹配项
【发布时间】:2016-11-10 03:11:20
【问题描述】:

我有以下一段日志文本,我想使用 # User@Host 的正则表达式对其进行拆分。我正在使用 Java 正则表达式库函数。

# Time: 160204  1:56:31
# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.000142  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
SET timestamp=1454579791;
SELECT DATABASE();
# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.001254  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
use test;
SET timestamp=1454579791;
# administrator command: Init DB;
# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.000441  Lock_time: 0.000077 Rows_sent: 4  Rows_examined: 4
SET timestamp=1454579791;
show databases;
# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.000207  Lock_time: 0.000074 Rows_sent: 1  Rows_examined: 1
SET timestamp=1454579791;
show tables;
# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.000537  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
SET timestamp=1454579791;
;

如果我这样做,我会得到以下 6 个字符串。

字符串 1:

# Time: 160204  1:56:31

字符串 2:

# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.000142  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
SET timestamp=1454579791;
SELECT DATABASE();

字符串 3:

# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.001254  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
use test;
SET timestamp=1454579791;
# administrator command: Init DB;

字符串 4:

# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.000441  Lock_time: 0.000077 Rows_sent: 4  Rows_examined: 4
SET timestamp=1454579791;
show databases;

字符串 5:

# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.000207  Lock_time: 0.000074 Rows_sent: 1  Rows_examined: 1
SET timestamp=1454579791;
show tables;

字符串 6:

# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.000537  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
SET timestamp=1454579791;
;

因此,使用 # User@Host 由正则表达式拆分会返回 6 个字符串。我实际上只对五个字符串感兴趣,这两个字符串首先组合在一起。所以,结果应该是这样的

字符串 1:

# Time: 160204  1:56:31
# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.000142  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
SET timestamp=1454579791;
SELECT DATABASE();

字符串 2:

# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.001254  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
use test;
SET timestamp=1454579791;
# administrator command: Init DB;

字符串 3:

# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.000441  Lock_time: 0.000077 Rows_sent: 4  Rows_examined: 4
SET timestamp=1454579791;
show databases;

字符串 4:

# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.000207  Lock_time: 0.000074 Rows_sent: 1  Rows_examined: 1
SET timestamp=1454579791;
show tables;

字符串 5:

# User@Host: root[root] @ localhost []  Id:     3
# Query_time: 0.000537  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
SET timestamp=1454579791;
;

我怎样才能做到这一点?

【问题讨论】:

    标签: java regex split


    【解决方案1】:

    您可以在拆分后附加第一个和第二个元素:

    String string1 = splitArray[0] + splitArray[1];
    

    当然,这只有在您知道格式始终是您列出的格式时才有效。为确保是这种情况,您可以添加如下所示的检查:

    if(splitArray[0].startsWith("# Time:"){
        String string1 = splitArray[0] + splitArray[1];
    }
    

    我确信有更优雅的方法可以实现这一点,但这会奏效;)

    【讨论】:

    • 谢谢,这适用于这种情况。但是,我想要更通用的东西。更详细一点 - 给定一段日志文本,一个正则表达式来捕获一些文本(#时间:160204 1:56:31在这种情况下从字符串1)和一个正则表达式拆分(比如#User@Host),我是尝试检查我们是否可以将文本 # Time: 160204 1:56:31 添加到所有没有它的字符串(即字符串 2 到字符串 5)。我遇到了问题,因为我无法弄清楚正则表达式的拆分,它只给我字符串 2 到字符串 5。
    • 继续...我想避免连接字符串 1 和 2 的需要(如果可能的话)(这里会分开),因为我不知道它是否可以通用。
    猜你喜欢
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 2021-09-22
    • 1970-01-01
    • 2011-03-06
    相关资源
    最近更新 更多