0

Im trying to set a validation for not repeated file names, if some file is repeated it should be writen this way:

  • FILENAME.PDF
  • FILENAME_1.PDF
  • FILENAME_2.PDF

So I can make the first one, but all the rest how can be taken?.

I will get a list of all filenames documents before, so now im trying to make like a for each document but i dont know how to make regex for get it.

FILENAME_1.PDF -> REGEX() -> GET RETURN -> 1


UPDATE

If I have this files in my bbdd: [filename.pdf, filename_1.pdf, filename_2.pdf].

I need when someone upload some new, based on that files, my new name will be filename_3.pdf if exist file name 2.

FILENAME_VERSION.EXTENSION

Base on this i need get the version of the last filename. Thanks!

Note: There will be more filesnames differents, for example FILANEM_FILENAM_VERSION.pdf


Thanks you!

Elio Lako
  • 1,333
  • 2
  • 16
  • 26
Alberto Acuña
  • 512
  • 3
  • 9
  • 28
  • 1
    Why do you want to do it with regex? Just use a hashmap with filename occurancies - . Don't overcomplicated the simple task. – Amongalen Mar 06 '19 at 15:22
  • Could you add the expected output for your example ? "So I can make the first one, but all the rest how can be taken ?" This sentence is unclear. You want to get all copy of the first, or only the first and ignore copy ? – vincrichaud Mar 06 '19 at 15:22
  • I cant use hashmap, only filename. @Amongalen – Alberto Acuña Mar 06 '19 at 15:27
  • I want to get the last version of copys, for create the new one, with the version increased. I will update my asnwer. @vincrichaud – Alberto Acuña Mar 06 '19 at 15:28
  • Why you `can't` use hashmap? Is it some kind of a homework or something? Maybe have a look at this - regex that fidns unique results: https://stackoverflow.com/questions/13613813/get-unique-regex-matcher-results-without-using-maps-or-lists – Amongalen Mar 06 '19 at 15:30
  • 1
    `_(\d+)\.[^.]+$` would extract the number (it captures the number that precedes the last dot in the filename and follows an underscore, you can try it [here](https://regex101.com/r/2A71wP/2)). I'm really not sure how that helps solving your problem though. – Aaron Mar 06 '19 at 15:32
  • How do you know that filename doesn't contain numbers in it? Can't a file be named `filename_1.pdf`? Its copy would be `filename_1_1.pdf` – Amongalen Mar 06 '19 at 15:34
  • thats why i need the regex, every file with NAME_version.PDF will be the same with version increased, so it should be filename_2.pdf and not filename_1_1.pdf @Amongalen – Alberto Acuña Mar 06 '19 at 15:35
  • not getting the version of filename, sry @Aaron is java regex? – Alberto Acuña Mar 06 '19 at 15:42
  • @AlbertoAcuña I've added an answer with an ideone link with java code – Aaron Mar 06 '19 at 15:46

1 Answers1

1

I would use the following regex :

_(\d+)\.[^.]+$

Or in Java :

_(\\d+)\\.[^.]+$

The regex captures a number between an underscore and the last dot of the filename.

The number you seek is captured in the first capturing group and needs to be extracted using Matcher.group.

You can try it here :

No version found in filename.pdf
No version found in filename1.pdf
filename_1.pdf - version found : 1
filename1_2.pdf - version found : 2
No version found in filename_1.test.pdf
filename_1.test_2.pdf - version found : 2
No version found in filename_1

Aaron
  • 24,009
  • 2
  • 33
  • 57