2

I want to know if a File has extension just using REGEX. Example

  • Hello.jpg (ok)
  • Goodbye.mp4 (ok)

  • Free.xlsx (ok)

  • GettingStarted (NO)

  • File.81723.domain.8080.pdf (ok)

How can i do this in Android?

I've tried this but is not working

if(!fileName.matches("(\\.[^\\\\]+)$"))
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
JaviSanchezG
  • 143
  • 1
  • 12

4 Answers4

2

You need to use a .* in the beginning as matches requires a full string match, and the negated character class should be [^.] to match any char but a literal ..

Use

if(!fileName.matches(".*\\.[^.]+")) ....

See the regex demo

Java demo:

String a[] = new String[]{"Hello.jpg","Goodbye.mp4","Free.xlsx","GettingStarted","File.81723.domain.8080.pdf"};
for (String s : a) {
if (s.matches(".*\\.[^.]+"))
    System.out.println(s + ": true");
else
    System.out.println(s + ": false");
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

@CommonsWare told me the solution and it is:

String[] tokens = fileName.split("\\.(?=[^\\.]+$)");

Then you get:

"test.cool.awesome.txt".split("\\.(?=[^\\.]+$)");

Yields:

["test.cool.awesome", "txt"]
Community
  • 1
  • 1
JaviSanchezG
  • 143
  • 1
  • 12
  • *I want to know if a File has extension just using REGEX.* - You do not really ask how a filename can be split into file name and extension in your question. – Wiktor Stribiżew May 27 '16 at 19:36
0

System.out.println("yourFileName.txt".split("\\.(?=[^\\.]+$)").length >1 ? "OK":"NO");

where, yourFileName.txt : Your file name.

split("\.(?=[^\.]+$)") : Splits the file name from the last dot.

If the array size is greater than 1 then it has an extension, otherwise not.

V33R
  • 909
  • 8
  • 10
0

For something as simple as detecting if a dot is in the string, you don't really have to use a regex. String API provides lastIndexOf(int) method:

lastIndexOf(int ch)

Returns the index within this string of the last occurrence of the specified character.

If you go through the method documentation, you'll notice that:

(...) if no such character occurs in this string, then -1 is returned. The String is searched backwards starting at the last character.

Basically, all you have to do is check if this method returns a value greater than -1 and it isn't the last character. For example:

public static boolean hasExtension(final String fileName) {
    final int indexOfDot = fileName.indexOf('.');
    // Checking if dot index is greater than 0 - omitting
    // names without dot or starting with a dot.
    return indexOfDot > 0 && indexOfDot < fileName.length() - 1;
}

public static String getExtension(final String fileName) {
    return hasExtension(fileName) ?
         // +1 in substring to strip the dot.
         fileName.substring(fileName.lastIndexOf('.') + 1)
         // Returning empty string if no extension.
         : "";
}

"file.pdf" will report as having an extension and return pdf. "file", ".file" and "file." will report as extensionless.

Czyzby
  • 2,999
  • 1
  • 22
  • 40
  • Some files have one or multiple dots (.) in their filename without having an extension, this was not working for me. – JaviSanchezG May 27 '16 at 19:50
  • @JaviSanchezG This approach has the same exact results, unless the file name starts with a dot. The regex will **not** detect if the extensions are in any given list of "accepted" extensions and will simply detect files which have at least 1 character before a dot and at least 1 character after a dot. I can update the answer to check if the dot is not the first character. – Czyzby May 27 '16 at 19:56
  • Look at this answer http://stackoverflow.com/a/37490942/3058053 in that example if the filename was "test.cool.awesome" (without txt) your method will tell me that "awesome" is the "extension" but with that regex it won't. You can test it. – JaviSanchezG May 27 '16 at 20:01
  • Both `"test.cool.awesome".matches(".*\\.[^.]+")` and `"test.cool.awesome.txt".matches(".*\\.[^.]+")` return `true`, so the regex assumes both **have** an extension. It's still the same behavior as the last index approach. – Czyzby May 27 '16 at 20:06
  • You are using matches, use split. in one you have 2 substrings and in the other case just one ;-) – JaviSanchezG May 27 '16 at 20:08
  • @JaviSanchezG `"test.cool.awesome".split("\\.(?=[^\\.]+$)")` returns `["test.cool", "awesome"]`... – Czyzby May 27 '16 at 20:09
  • Yes! You're right, i was testing with a file with multiple dots in the name and the extension too. So is ok, in that case your answer will work too. How can i add the condition of matching just the cases that are 3 or 4 letters after the last dot? – JaviSanchezG May 27 '16 at 20:22
  • You could try something like `(indexOfDot > fileName.length() - 5 && indexOfDot < fileName.length() - 1)`, but it will still accept incorrect extensions. There's only so much you can do with simple dot position check. I suggest a different approach - extract the extension from file name and keep a `Set` of accepted extensions. Use my `getExtension(String)` method and check if the result is in the set with `contains(Object)` method. – Czyzby May 27 '16 at 20:28