Reputation: 655
I am trying to valid a given folder name using a regular expression, I tried some tests and it works fine, please let me know if I am totally safe or my reg expression miss something:
QRegExp regExp("^( [a-zA-Z] )( ( [a-zA-Z_\-\s0-9\.\)\(] )( [^\\!@#$%^&/:*?<>""|]* ) )*$" );
QStringList ForbidenNameU;
ForbidenNameU<<"CON"<<"PRN"<<"AUX"<<"NUL"<<"COM"<<"COM1"<<"COM2"<<"COM3"<<"COM4"<<"COM5"<<"COM6"<<"COM7"<<"COM8"<<"COM9"<<"LPT"<<"LPT1"<<"LPT2"<<"LPT3"<<"LPT4"<<"LPT5"<<"LPT6"<<"LPT7"<<"LPT8"<<"LPT9";
QStringList ForbidenName;
ForbidenName<<"con"<<"prn"<<"aux"<<"nul"<<"com"<<"com1"<<"com2"<<"com3"<<"com4"<<"com5"<<"com6"<<"com7"<<"com8"<<"com9"<<"lpt"<<"lpt1"<<"lpt2"<<"lpt3"<<"lpt4"<<"lpt5"<<"lpt6"<<"lpt7"<<"lpt8"<<"lpt9";
if((Dirname->text().contains(regExp))&& !ForbidenNameU.contains(Dirname->text())&& !ForbidenName.contains(Dirname->text()))
{
validname=true;
qDebug()<<"match in dir name = "<<regExp.cap(0);
emit completeChanged();
}...
PS(I don't want to use GetInvalidFileNameChars()
or any other api )
Thank you in advance.
Upvotes: 0
Views: 5333
Reputation: 153957
It's certainly wrong for some systems, as what is a valid directory name will depend on the system, and possibly even the actual file sy stem: remotely mounted file systems will typically have to obey the conventions for the file system host, and not for the local system.
Faced with this, there are two solutions, depending on the actual
problem you want to solve. The simplest one is just to try to create
the file, and check the error returned. This will give you a quick and
easy check whether the name is legal in the current environment. If you
need to create a hierarchy which can later be read anywhere, however,
the best solution is to take a very conservative approach, only allowing
alphanumeric characters and an '_'
, or possibly even only lower case
and digits (to avoid any risk of accidentally creating two directories
whose names differ only in case). You might also want to limit the
length. (Historically, there have been systems which only allowed six
characters, but that sounds a bit excessive today.)
With regards to the forbidden names: this is also very system dependent,
and likely to depend on where the directories are being created. (There
are no reserved names under Unix, but you're almost certainly going to
run into trouble if you try to use certain names in specific
directories: "local"
is fine in general, but I wouldn't try to create
it under "/usr/"
.)
Upvotes: 0
Reputation: 43683
In Windows environmnent:
^(?!(?:CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(?:\.[^.]*)?$)[^<>:"/\\|?*\x00-\x1F]*[^<>:"/\\|?*\x00-\x1F\ .]$
with CASE_INSENSITIVE and UNICODE_CASE modifiers.
^ # Anchor to start of string.
(?! # Assert filename is not: CON, PRN,
(?: # AUX, NUL, COM1, COM2, COM3, COM4,
CON|PRN|AUX|NUL| # COM5, COM6, COM7, COM8, COM9,
COM[1-9]|LPT[1-9] # LPT1, LPT2, LPT3, LPT4, LPT5,
) # LPT6, LPT7, LPT8, and LPT9...
(?:\.[^.]*)? # followed by optional extension
$ # and end of string.
) # End negative lookahead assertion.
[^<>:"/\\|?*\x00-\x1F]* # Zero or more valid filename chars.
[^<>:"/\\|?*\x00-\x1F\ .] # Last char is not a space or dot.
$ # Anchor to end of string.
Upvotes: 4