Yes, check for failure. But regular expressions to perfectly secure data passed to the shell are actually pretty easily come by. You just need to remember the cardinal rule, deny everything that is not explicitly allowed
. Trying to trap everything that can go wrong is hard. Allowing very limited input, is not. Try the following:
(?:\w+\/)* # Directory components?
\w+ # Start of filename
(?:\.\w+)? # Extension?
This will work for most filenames you have a reason to allow, and I guarantee you that on a standard Unix system with a standard directory structure (unless someone has placed poor symlinks, etc), there is no way to name anything that passes this which has shell problems.
You could test length($1) if you are afraid of buffer overflows. :-)
What you will find though is that at some point some developer wants to break the rule. That is fine, just become more lenient (eg allow an optional period in a directory) but step by step keep the philosophy the same. Only that which you have guaranteed safe shall pass.