Remove specific words in a string in java

It'll work if filename contains digits not only in date part. I assume that date is always appears before extension and has fixed length.

s.replaceAll("\\d{8}\\.pdf", ".pdf");

And if the file extension varies then you could do some additional work:

public static String removeDate(String s) {
    final String extension = s.substring(s.lastIndexOf("."));
    final String pattern = "\\d{8}\\" + extension;

    return s.replaceAll(pattern, extension);

public static void main(String args[])

You can use a regex to remove consecutive digits that resemble a date in any format provided the filename is appended immediately by the date.

"test20190906.pdf".replaceAll("[0-9]{8}\\.", "."));