2014-09-03 32 views
-5

我有這樣如何用特定字符替換一組重複的String模式?

採樣輸入字符串

00000000255255255255000000000255255000000000000002552552552552552552550000000000 

我需要使用至REGx

樣本輸出

00000000000,0000000000000000,000000000000000,0000000000 

假設如下替換此字符串代碼是這樣的

s="00000000255255255255000000000255255000000000000002552552552552552552550000000000"; 
s.replace("regularexpression",","); 
+2

要刪除所有非零數字?向我們展示示例輸出。 – TheLostMind 2014-09-03 13:27:50

+1

你是否想用','替換所有'不是0的一個或多個'數字'(範圍是1-9')?如果是這樣的話,爲什麼在你的結果中有更多的零比在你的輸入? – Pshemo 2014-09-03 13:30:44

+0

@TheLostMind它不是那麼簡單,我猜,請注意輸出中零的數量已更改。 – Kent 2014-09-03 13:30:55

回答

3

我猜你的樣本輸出是錯誤的。 ...

如果您想要替換所有連續的非零數字組用逗號的字符串,試試這個:

s = s.replaceAll("[1-9]+", ","); 

如果你想替換重複「255」一次或多次用逗號分隔的所有子,試試這個:

s = s.replaceAll("(255)+", ","); 
1

如果你想拆就非0多個數字,這裏是一個解決方案:

String input = "00000000255255255255000000000255255000000000000002552552552552552552550000000000"; 
//     | String representation of the split array 
//     |      | splitting... 
//     |      |  |... on a character class... 
//     |      |  || ...for any digit non-0 
//     |      |  || | in 1+ sequential instances 
System.out.println(Arrays.toString(input.split("[1-9]+"))); 

輸出

[00000000, 000000000, 00000000000000, 0000000000] 
+0

它與OP給出的輸出不匹配...'00000000000,0000000000000000,000000000000000,0000000000' – Kent 2014-09-03 13:32:31

+0

@Kent剛剛注意到,正在修復。謝謝 – Mena 2014-09-03 13:33:22

+1

我很好奇,如果你能解決它....我根本不明白的要求。 :-D – Kent 2014-09-03 13:35:15

相關問題