Detect JPEG correctly

by Ricardo Fernández Serrata

Version 1 (September 13, 2021)

Download (4 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.

Explanation:
.* : 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

4.0 average rating from 1 reviews

5 stars
0
4 stars
1
3 stars
0
2 stars
0
1 star
0
Reports
0

Rate and review within the app in the Community section.