如何使用正则表达式验证图像文件扩展名?
介绍
在本文中,我们检查如何使用正则表达式验证图像文件扩展名。本文中的图像文件扩展名是指包含文件名和文件扩展名的有效图像文件扩展名。有效的图像文件扩展名遵循我们在本文中定义的一些规则。
正则表达式或正则表达式用于字符串中的模式匹配或字符串搜索算法。其功能在 <regex> 头文件中定义。它与一个后跟变量的 regex 关键字一起使用。在本 C++ 教程中,我们使用正则表达式检查输入字符串是否为有效的图像文件扩展名。我们考虑一个输入字符串,并通过匹配图像文件扩展名命名规则来检查其有效性。为了验证图像文件扩展名,我们使用正则表达式进行模式匹配。
语法
regex regular_expression_patternname return regex_match(value, regular_expression_patternname);
有效图像文件扩展名的规则如下:
图像文件应以至少一个字符开头。
图像文件不能为空。
图像文件扩展名后跟一个点和文件名扩展名。例如,abc.jpg
图像文件名称中没有空格。
可接受的图像文件扩展名是 .jpg、.gif、.jpeg、.bmp 和 .png。
演示 1
Input = Filename = "cdf.jpeg" Output = The input name is a valid file name.
解释
以上字符串遵循所有有效图像文件的规则。因此,它是一个有效的图像文件扩展名。
演示 2
Input = Filename = "6fg.jpfg" Output = No, it is not a valid image file.
解释
根据图像文件规则,以上输入字符串是无效的图像文件。有效的图像文件扩展名没有 jpfg 扩展名。
因此,它是一个无效的图像文件。
算法
获取一个输入图像文件字符串。
定义一个包含所有有效文件扩展名规则的正则表达式。
定义正则表达式后,迭代输入字符串的所有字符。
将输入字符串的每个字符与正则表达式模式匹配。
使用条件语句确定结果。
打印输出。
正则表达式的语法
正则表达式如下所示:
^[^\s]+\.(jpg|jpeg|png|gif|bmp)$
这里,
^ = 它表示字符串的开头。
[^\s]= 它表示应以字符开头的字符串。^ 是否定字符类。//s 表示没有空格
+ = 它表示字符串可以有多个字母。
\. = 反斜杠 (\) 用于赋予点 (.) 意义,因为这里点仅被视为点。
(jpg|jpeg|png|gif|bmp) = 它是有效图像文件扩展名的组。竖线 (|) 充当 OR 运算符,在不同的图像文件扩展名之间提供选择。
$ = 它表示字符串的结尾。
示例 1
我们在 C++ 中实现了查找有效输入图像文件扩展名的任务。通过生成包含所有有效图像文件规则的 match_pattern 来使用正则表达式。
#include <iostream>
#include <regex>
#include <string>
using namespace std;
//user-defined function to determine the validity of the input string using regular expression
bool isImageFileValid(const string& filename) {
// Regular expression for a valid image file. It satisfy all the conditions of a valid image file
regex imageRegex("^[^\s]+\.(jpg|jpeg|png|gif|bmp)$", regex_constants::icase);
// Match the image file extension against the regular expression
return regex_match(filename, imageRegex);
}
//code controller
int main() {
// Predefined image file name
string filename = "abc.ppt";
//conditional statement to determine the result via calling the function
if (isImageFileValid(filename)) {
cout << "Valid image file!" << endl;
} else {
cout << "Invalid image file extension or filename format!" << endl;
}
return 0;
}
输出
Invalid image file extension or filename format!
示例 2
我们在 C++ 中使用正则表达式实现了检查输入字符串是否为有效图像文件的任务。使用了不同的输入字符串。利用了一个包含所有有效图像文件条件的正则表达式。
#include <iostream>
#include <regex>
using namespace std;
// user-defined function to verify the string
bool validImageFile(string strVar){
// Regular expression containing all conditions for a valid image file extension
const regex matchPattern("[^\s]+(.*?)\.(jpeg|jpg|png|gif|JPEG|JPG|GIF|PNG)$");
// If the string is empty
if (strVar.empty()) {
return false;
}
// conditional statement that returns true when a string is a valid image file extension
if(regex_match(strVar, matchPattern)) {
return true;
} else {
return false;
}
}
// Code controller
int main(){
string strVar1 = "1de.jpeg";
cout << validImageFile(strVar1) << endl;
string strVar2 = "cgd.jpg";
cout << validImageFile(strVar2) << endl;
string strVar3 = "cfg.pnfg";
cout << validImageFile(strVar3) << endl;
string strVar4 = ".jpg";
cout << validImageFile(strVar4) << endl;
string strVar5 = "a b.jpg";
cout << validImageFile(strVar5) << endl;
return 0;
}
输出
1 1 0 0 1
结论
我们已经完成了本教程,在本教程中,我们检查输入字符串是否为有效的图像文件扩展名。我们使用正则表达式来检查字符串的有效性。根据有效图像文件扩展名的条件,我们声明了一个正则表达式并将其与输入字符串匹配。使用条件语句确定结果。用一些示例演示了问题陈述,以确定任务的需求。
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP