by Ricardo Fernández Serrata
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")
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
Rate and review within the app in the Community section.