-2

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.

Ives
  • 505
  • 1
  • 4
  • 14
  • Just split on the last underscore? – Rob Audenaerde Aug 15 '18 at 07:23
  • If you want just the last number-part you can use substring in combination with lastIndexOf: filename.substring(filename.lastIndexOf("_")+1); – Ralf Renz Aug 15 '18 at 07:25
  • @RobAu Sorry, I didn't make it clear, underscore isn't necessary for the name. – Ives Aug 15 '18 at 07:34
  • So the name can be anything? Or will it end with a number? Or contain a number? See here: https://stackoverflow.com/questions/372148/regex-to-find-an-integer-within-a-string – Rob Audenaerde Aug 15 '18 at 07:36
  • @RobAu It will end with a number. – Ives Aug 15 '18 at 07:38
  • 1
    Possible duplicate of [Regular expression to match last number in a string](https://stackoverflow.com/questions/5320525/regular-expression-to-match-last-number-in-a-string) – Rob Audenaerde Aug 15 '18 at 07:39
  • What is it you want do achieve here? Do you want to import only specific files whose names follows a pattern or do you want to import multiple files and determine which ones belongs together based on their names? – Joakim Danielson Aug 15 '18 at 07:55

1 Answers1

2

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);
Goblin
  • 54
  • 3