没有用正则表达式,比较原始的方法
比如说文件格式为“IMG_20161214_000001.jpg”,中间为日期格式,最后为数字编号,要判断一个文件的名称是否符合上述规则。
输入:文件的名称
输入:符合规则返回true,不符合返回false
说明:将输入的名称字符串按照下划线“_”分割,分成3段,依次判断这3段是否符合要求
bool UDiskUserManager::checkFileName(char *fileName)
{
QString strFileName(fileName);
QStringList list = strFileName.split("_");
int digit =0;
if(list[0] == "IMG") //第一段
{
QString strDate = list[1];
if(strDate.size() == 8)
{
int year = strDate.left(4).toInt();
int month = strDate.mid(4,2).toInt();
int day = strDate.left(2).toInt();
if( (year>1900) && (year <2099) && (month >0) && (month <13) && (day < 32)) //第二段
{
QString strCount = list[2].left(6);
if(strCount.size() == 6)
{
char cCount[6] = {strCount.at(0).toLatin1(), strCount.at(1).toLatin1(), strCount.at(2).toLatin1(),strCount.at(3).toLatin1(),strCount.at(4).toLatin1(),strCount.at(5).toLatin1() };
for(int i=0; i<6; i++)
{
if( (0<=cCount[i]) || (cCount[i]<= 9)) //第三段,依次判断每一位是否为数字
{
digit++;
continue;
}
else
{
return false;
}
}
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
if(digit == 6)
{
return true;
}
}
方法比较笨,如果有更好的办法,还请指正。
|