string - Removing duplicate extension files on Java -


having folder following files

myoutput.pdf hello.pdf.pdf byebye.txt hey.txt.txt 

how can remove extension of files duplicated extension? in example there pdf , txt, in real example can type of extension.

i got far:

string dir = "c:/users/test";  file[] files = new file(dir).listfiles();  (file file : files) {     if (file.isfile()) {         system.out.println(file.getname());     } } 

but don't know how can detected ones "duplicated extension" , rename "single extension"

my desired output be:

myoutput.pdf hello.pdf byebye.txt hey.txt 

thanks in advance.

you may use

file.getname().replaceall("(\\.\\w+)\\1+$", "$1") 

see regex demo

pattern details:

  • (\\.\\w+) - group 1 capturing dot , 1 or more word chars (\\w+ might replaced [^.]+ match 1 or more chars other dot)
  • \\1+ - 1 or more occurrences of same value captured in group 1
  • $ - end of string.

the replacement pattern backreference group 1 ($1) have 1 single occurrence of extension.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -