2016-10-25 31 views
0

我試圖根據分隔符'將字符串分隔成多行,但如果在'之前有?字符,我希望數據保留在同一行中。如何根據分隔符將字符串分隔爲多行,但如果前面加上'?'則不能。

初始字符串:

HJK'ABCP 'QR2'SER'

我能夠打印像行: '?
ABCP'

HJK
QR2'
SER '

但我想輸出爲:'?
ABCP '

HJK QR2'
SER」

+3

顯示您的代碼作爲基礎來解決問題 –

+1

哪裏是你的代碼? – RealSkeptic

+0

string1.split(「(?')」);嘗試使用分割功能 –

回答

0

你需要一個負向後看(http://www.regular-expressions.info/lookaround.html):

String str = "HJK'ABCP?'QR2'SER'"; 
System.out.println(str); 
System.out.println("---------------"); 
System.out.println(str.replaceAll("'", "'\r\n")); 
System.out.println("---------------"); 
System.out.println(str.replaceAll("(?<!\\?)'", "'\r\n")); 

它返回:

HJK'ABCP?'QR2'SER' 
--------------- 
HJK' 
ABCP?' 
QR2' 
SER' 

--------------- 
HJK' 
ABCP?'QR2' 
SER' 
0

使用這個表達式(?<!\?)'在分裂funtion

0
String s="HJK'ABCP?'QR2'SER'"; 

     System.out.println(s.replaceAll("(?<!\\?)'","\r\n")); 
+0

您應該替換爲「'\ r \ n」。 「否則就會丟失 –

相關問題