user3191304
user3191304

Reputation: 221

How to get rid of numbers in the String?

I have a very long String. I need to get rid of numbers there

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
   ............

I need it to be

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

I tried the following

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

Upvotes: 1

Views: 225

Answers (2)

Elliott Frisch
Elliott Frisch

Reputation: 201439

I would use something like this

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);
}

Which outputs

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

Upvotes: 2

Pshemo
Pshemo

Reputation: 124215

Maybe you are looking for something like

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

This regex will search for lines (character between start of line ^ and end of line $) that are either

  • \\s spaces
  • \\d digits
  • :
  • ,
    or
  • -->

(?m) is "multiline" flag used to let ^ and $ be start or end of each line, instead of entire string.

Upvotes: 3

Related Questions