如何摆脱字符串中的数字?

时间:2014-01-15 01:32:52

标签: java regex

我有一个非常长的字符串。我需要摆脱那里的数字

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","");

2 个答案:

答案 0 :(得分:3)

也许你正在寻找像

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

此正则表达式将搜索

中的行(行^的开头和行$行的结尾之间的字符)
  • \\s空格
  • \\d位数
  • :
  • ,
  • -->

(?m)是“ multiline ”标志,用于让^$成为每行的开头或结尾,而不是整个字符串。

答案 1 :(得分: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