Search code examples
javafilenamessequential

Detecting files with sequential number in the file name


I want to implement a function that imports multiple files with the sequential number.
If the file names are something like aaa_000, aaa_001 than I can split them by the underscore and take the number. But when the file names aren't in this pattern, I don't know what is the good way to do this.
File names could be a_aa_000, a00a_000, a_0_000 , aaa000, a_aa000 and they end with a number.


Update:
I found a way to achieve it by finding the last not number char, the chars before it will be the mutual name.


Solution

  • Adding to what @Robabu mentioned, please change your pattern to include a "$" so that it only matches the pattern ending with the string. Below code works for all the test inputs you mentioned above.

    Pattern intsOnly = Pattern.compile("\\d+$");
    Matcher makeMatch = intsOnly.matcher("a_aa000");
    makeMatch.find();
    String inputInt = makeMatch.group();
    System.out.println(inputInt);