Sanitizing strings with file names and extension in Java

There are four types of file names:

  • Double extension file name
  • File name without extension
  • File name with a dot at the end and without extension
  • A file name with a proper name.

Like this:

String doubleexsension = "doubleexsension.pdf.pdf";
String noextension = "noextension";
String nameWithDot = "nameWithDot.";
String properName = "properName.pdf";

String extension = "pdf";

My goal is to clear all types and correctly output only filename.filetype. I made a little silly script to make this post:

ArrayList<String> app = new ArrayList<String>();
app.add(doubleexsension);
app.add(properName);
app.add(noextension);
app.add(nameWithDot);

System.out.println("------------");

for(String i : app) {

    // Ends with .
    if (i.endsWith(".")) {
        String m = i + extension;
        System.out.println(m);
        break;
    }

    // Double extension
    String p = i.replaceAll("(\\.\\w+)\\1+$", "$1");
    System.out.println(p);
}

It is output:

------------
doubleexsension.pdf
properName.pdf
noextension
nameWithDot.pdf

I do not know how I can handle noextension. How should I do it? When there is no extension, it should take a value extensionand bind it to the line at the end.

My desired result:

------------
doubleexsension.pdf
properName.pdf
noextension.pdf
nameWithDot.pdf

Thanks in advance.

+4
source share
5 answers

:

(?:(\.\w+)\1*|\.|([^.]))$

$2.pdf. . regex demo.

. , , , :

(?:(\.(?:pdf|gif|jpe?g))\1*|\.|([^.]))$

- regex.

  • (?: - , $ ( )
    • (\.\w+)\1* - ( ) (. + 1+ ) ( - (?:pdf|gif|jpe?g) pdf, gif, jpeg , jpg .., )
    • | -
    • \. -
    • | -
    • ([^.]) - char, , 2
  • ) -
  • $ - .

Java-:

List<String> strs = Arrays.asList("doubleexsension.pdf.pdf","noextension","nameWithDot.","properName.pdf");
for (String str : strs)
    System.out.println(str.replaceAll("(?:(\\.\\w+)\\1*|\\.|([^.]))$", "$2.pdf"));
+4

if (-1 == i.indexOf('.'))
    System.out.println(i + "." + extension);
+2

I would avoid the complexity (and reduced readability) of regular expressions:

String m = i;

if (m.endsWith(".")) {
    m = m + extension;
}
if (m.endsWith("." + extension + "." + extension)) {
    m = m.substring(0, m.length() - extension.length() - 1);
}
if (!m.endsWith("." + extension)) {
    m = m + "." + extension;
}
+1
source

Why so hard. Just dostr.replaceAll("\\..*", "") + "." + extension

0
source

Java 7 NIO has a way to do this using PathMatcher

PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:*.pdf");

Path filename = namewithdot.pdf;
if (matcher.matches(filename)) {
    System.out.println(filename);
}
-1
source

Source: https://habr.com/ru/post/1660400/


All Articles