【发布时间】: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;
;
我怎样才能做到这一点?
【问题讨论】: