How to validate image file extension using Regular Expression?


Introduction

In this article, we check validate image file extension using regular expressions. Image file extension in this article, is the valid extension for an image file which consists of file name and file extension . The valid image file extension follows some rules which we define in this article.

Regular expression or regex is used for pattern matching in strings or string searching algorithms. Its functionalities are defined in the <regex> header file. It is used with a regex keyword followed by a variable. In this C++ tutorial, we check whether the input string is a valid image file extension or not using a regular expression. We consider an input string and check its validation by matching the image file extension naming rules. To validate image file extension we use pattern matching through regular expression.

Syntax

regex regular_expression_patternname
return regex_match(value, regular_expression_patternname);

Rules for a valid image file extension are as follows −

  • The image file should start with at least one character.

  • The image file cannot be empty.

  • The image file extension is followed by a dot and filename extension. For example, abc.jpg

  • There is no white space in the name of the image file.

  • The acceptable image file extensions are .jpg, .gif, .jpeg, .bmp, and .png.

Demonstration 1

Input = Filename = "cdf.jpeg"
Output = The input name is a valid file name.

Explanation

The above string follows all the rules of the valid image file. So, it is a valid image file extension.

Demonstration 2

Input = Filename = "6fg.jpfg"
Output = No, it is not a valid image file.

Explanation

The above input string is an invalid image file as per the image file rules. A valid image file extension does not have jpfg extension.

Hence, it is an invalid image file.

Algorithm

  • Take an input image file string.

  • Define a regular expression that contains all the rules of the valid file extension.

  • After defining a regular expression, iterate all the characters of the input string.

  • Match each character of the input string with the regex pattern.

  • Use conditional statements to determine the result.

  • Print the Output.

Syntax of Regular Expression

The regular expression is like this −

^[^\s]+\.(jpg|jpeg|png|gif|bmp)$

Here,

  • ^ = It represents the start of the string.

  • [^\s]= It represents the string that should start with a character. ^ It is negated character class. //s means no white space

  • + = It means a string can have more than one alphabet.

  • \. = the backslash (\) is used to give meaning to the dot (.) as here dot is treated as a dot only.

  • (jpg|jpeg|png|gif|bmp) = It is a group of valid image file extensions. The vertical bar (|) works as an OR operator, giving choices among different image file extensions.

  • $ = It represents the end of the string.

Example 1

We implement the task of finding a valid input image file extension in C++. Use regular expressions by generating a match_pattern that contains all the rules of a valid image file.

#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;
}

Output

Invalid image file extension or filename format!

Example 2

We implement the task of checking if the input string is a valid image file or not with regular expressions in C++. Used different input strings. Utilized a regular expression that contains all the conditions for a valid image file.

#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;
}

Output

1
1
0
0
1

Conclusion

We have reached the end of this tutorial where we check whether the input string is a valid image file extension. We used regular expressions to check the string's validity. According to the conditions of valid image file extensions, we declared a regular expression and matched it with the input string. Use conditional statements to determine the result. Demonstrated the problem statement with some examples to determine the needs of the task.

Updated on: 03-Oct-2023

774 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements