2015-02-05 22 views
-1

我寫了一個Java類,接收文本,解析它並將其存儲在MySQL數據庫中。Java的MySQL插入產生一個很長的線 - 正則表達式

在大多數情況下,結果是很好的解析,但有時我得到如下結果:

這是我的主要問題!看起來我有很多空間讓我的插件如此之大。

+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
| mail_text                                                                                                                                                                                                                               | 
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
| Amt 
Security B S A S Bench B Px A Px B Z A Z A YT Out Mood S&P Notes 
-------------------------------------------------------------------------------- 
GS 4½ 16 51/46 OBL 2¾ 16 105.43-105.50 23/18 0.34 1.25MM Baa1 A- 

{IMGR<GO>} 

      | 
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 

這麼看來,我的正則表達式是不是那麼好,因爲它可能之前與其做這麼長的行終止。

下面你會發現我的正則表達式:

 parsedBody = body.toString().replaceAll("(?m)(^ *| +(?= |$))", "").replaceAll("(?m)^$([\r\n]+?)(^$[\r\n]+?^)+", "$1"); 

這應該刪除兩個以上的下列空間,只有一個替換和刪除所有的空行:

"  This is the example 


of what I want to achieve " 

結果應該是:

"This is the example 
of what I want to achieve" 

更新: @ 我嘗試了你的正則表達式,但我仍然沒有得到我想要的結果。 這是我通過電子郵件收到一個例子:

** GS ARGID 4¼'22 98¼/99 || 8⅜'19 96/97 











        ARDAGH 

這應該是:

** GS ARGID 4¼'22 98¼/99 || 8⅜'19 96/97 
ARDAGH 

我將開放供任何意見!

+0

[MySQL sucks](https://duckduckgo.com/?q=mysql+sucks)。 [選擇其他](http://grimoire.ca/mysql/choose-something-else)。 – Unihedron 2015-02-08 07:48:55

回答

0

這將適用於你的情況。

String s = "  This is the example\n" + 
       "\n" + 
       "\n" + 
       "of what I want to achieve "; 
System.out.println(s.replaceAll("([\\r\\n])+", "$1").replaceAll("(?m)^ +| +$|(?<=) +", "")); 

輸出:

This is the example 
of what I want to achieve 

([\\r\\n])+匹配一個或多個換行符,只有抓住了最後。

+0

我試過你的正則表達式,但是我沒有達到預期的結果,我在我的問題中更新了一個例子。我還有第一個結果的問題,我在MySQL表中有非常長的文本,我希望它在「notes」字段後結束 – Max 2015-02-06 18:13:21

0

你實際上試圖在這裏實現三個獨立的事情:1)用一個替換多個換行符,2)用一個替換多個空格字符,3)修剪字符串。要實現全部三項,你必須這樣做:

String test = "  This is the example\n" + 
     "\n" + 
     "\n" + 
     "of what I want to achieve "; 
String result = test.replaceAll("("+System.getProperty("line.separator")+")+", "$1").replaceAll("[ ]+", " ").trim();