2014-01-15 117 views
1

我有一個很長的字符串。我需要擺脫數有如何擺脫字符串中的數字?

And we're never gonna 
bust out of our cocoons 

65 
00:03:04,113 --> 00:03:06,815 
- if we don't put our busts out there. 
- Nice metaphor. 

66 
00:03:06,833 --> 00:03:09,418 
And we can just go to 
the piano bar and not sing 
    ............ 

我需要它是

And we're never gonna 
bust out of our cocoons 

- if we don't put our busts out there. 
- Nice metaphor. 

And we can just go to 
the piano bar and not sing 

我嘗試以下

myString = myString.replaceAll("\d+\n\d",""); 
+0

使用字符類。方括號 –

+0

你應該清楚你的要求。你說你想擺脫數字,那麼爲什麼': - >'等也被刪除?什麼是你想要的實際規則?刪除一些東西,如果它以數字開始和結束?如果不包含字母表,則刪除該行?或者只有按照確切的格式才能刪除它? –

回答

3

也許你正在尋找的東西像

myString = myString.replaceAll("(?m)^([\\s\\d:,]|-->)+$", ""); 

這個正則表達式會搜索行(c線^和線$的端的開始之間haracter),其或者是

  • \\s空間
  • \\d
  • 的數字
  • :
  • ,
  • -->

(?m)是「多行」標誌用於讓^$爲每行的開始或結束,而不是整個字符串。

+0

我不確定'.............'是否是你的字符串的一部分,所以沒有將它包含在答案中。 – Pshemo

2

我會用這樣的

public static void main(String[] args) { 
    String pattern = "[0-9]+\n[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9] " 
     + "--> [0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9]\n"; 
    String in = "And we're never gonna\n" 
     + "bust out of our cocoons\n\n65\n" 
     + "00:03:04,113 --> 00:03:06,815\n" 
     + "- if we don't put our busts out there.\n" 
     + "- Nice metaphor.\n\n66\n" 
     + "00:03:06,833 --> 00:03:09,418\n" 
     + "And we can just go to\n" 
     + "the piano bar and not sing"; 
    in = in.replaceAll(pattern, "\n").replace("\n\n", 
     "\n"); 
    System.out.println(in); 
} 

,輸出

 
And we're never gonna 
bust out of our cocoons 

- if we don't put our busts out there. 
- Nice metaphor. 

And we can just go to 
the piano bar and not sing 
+0

不錯,全面 – CHEBURASHKA