Detect JPEG correctly

by Ricardo Fernández Serrata

Version 1 (October 24, 2021)

Download (31 downloads)

This demonstrates how to check if a filesystem path is a JPEG file or not, using a simple regex without bugs.

This assumes the filename extension corresponds to the format of the data within the file. If you want to be 100% sure the file contents are JPEG, use the "file" shell command.

.* : any substring at the beginning
[^/] : any char BUT slash
\.jpe?g : .jpg or .jpeg ending

If we used this:
glob(path, "*.jpg") || glob(path, "*.jpeg")

Or this:
matches(path, ".*\\.jpe?g")

It would be correct in most cases, except .jpeg and .jpg, because they will be detected as JPEG files when they aren't, because any file or directory whose name begins with "." is just hidden, not an extension. This is why "[^/]" is necessary.

.jpg.jpg is a JPEG file whose name is "jpg" and it's hidden because of the 1st "."

Fun fact: if you replace "jpe?g" with "html?" in the regex, it does the same thing but only for HTML files.

Edit: I forgot to use "DotAll" flag. Replace the regex with this "(?s).*[^/]\\.jpe?g". This has better performance and less prone to bugs

4.0 average rating from 1 reviews

5 stars
4 stars
3 stars
2 stars
1 star

Rate and review within the app in the Community section.