2016-11-10 10 views
0

次のログテキストがあります。正規表現#User @ Hostを使用して分割したいと思います。私はJava regex lib関数を使用しています。最初の一致を無視して正規表現を使用してログテキストを分割します

# Time: 160204 1:56:31 
# [email protected]: root[root] @ localhost [] Id:  3 
# Query_time: 0.000142 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0 
SET timestamp=1454579791; 
SELECT DATABASE(); 
# [email protected]: 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; 
# [email protected]: root[root] @ localhost [] Id:  3 
# Query_time: 0.000441 Lock_time: 0.000077 Rows_sent: 4 Rows_examined: 4 
SET timestamp=1454579791; 
show databases; 
# [email protected]: root[root] @ localhost [] Id:  3 
# Query_time: 0.000207 Lock_time: 0.000074 Rows_sent: 1 Rows_examined: 1 
SET timestamp=1454579791; 
show tables; 
# [email protected]: 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:

# [email protected]: 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:

# [email protected]: 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:

# [email protected]: 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:

# [email protected]: 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:

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

だから、正規表現によって分割として#ユーザ@ホストを使用すると、バック6弦を与えます。私は実際には5つの文字列に興味があり、最初は2つの文字列を結合します。だから、結果は

文字列1のようになります。

# Time: 160204 1:56:31 
# [email protected]: 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:

# [email protected]: 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:

# [email protected]: 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:

# [email protected]: 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:

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

私は、これを達成できますか?あなただけの分割後に第1および第2の要素を追加することができ

答えて

0

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

は確かにあなたはフォーマットはいつもあなたが記載されているものをされることがわかっている場合、これはのみ動作します。これはあなたのように見えるかもしれませんチェック追加できる場合であることを保証するために:私はこれを実現するために、よりエレガントな方法があると確信しているが、これは動作します

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

を;)

+0

おかげで、この希望この場合の仕事。しかし、私はより一般的な何かを持っていたいと思います。もう少し詳しく説明するために、ある文字列を与えられた正規表現(#Time:160204 1:56:31、この場合は文字列1)と正規表現による分割(#User @ Host)があります。テキスト#Time:160204 1:56:31をそれを持たないすべての文字列(つまり、文字列2から文字列5まで)に付加することができるかどうかをチェックしようとします。私は文字列2に文字列2を与える正規表現による分割を見つけることができないので問題に遭遇しています。 –

+0

続き...私は文字列1と2を連結する必要性を避けたいと思います。私はそれが一般的に動作するかどうかわからないので、ここで分裂するだろう)。 –

関連する問題