How to validate image file extension using Regular Expression
Last Updated :
26 Dec, 2022
Given string str, the task is to check whether the given string is a valid image file extension or not by using Regular Expression.
The valid image file extension must specify the following conditions:
- It should start with a string of at least one character.
- It should not have any white space.
- It should be followed by a dot(.).
- It should be end with any one of the following extensions: jpg, jpeg, png, gif, bmp.
Examples:
Input: str = “abc.png”
Output: true
Explanation:
The given string satisfy all the above mentioned conditions.
Input: str = “im.jpg”
Output: true
Explanation:
The given string satisfy all the above mentioned conditions.
Input: str = “.gif”
Output: false
Explanation:
The given string doesn’t start with image file name(required at least one character). Therefore, it is not a valid image file extension.
Approach: This problem can be solved by using regular expression.
- Get the String.
- Create a regular expression to check the valid image file extension as mentioned below:
regex = “([^\\s]+(\\.(?i)(jpe?g|png|gif|bmp))$)”;
- Where:
- ( represents the starting of group 1.
- [^\\s]+ represents the string must contain at least one character.
- ( represents the starting of group 2.
- \\. Represents the string should follow by a dot(.).
- (?i) represents the string ignore the case-sensitive.
- ( represents the starting of group3.
- jpe?g|png|gif|bmp represents the string end with jpg or jpeg or png or gif or bmp extension.
- ) represents the ending of the group 3.
- ) represents the ending of the group 2.
- $ represents the end of the string.
- ) represents the ending of the group 1.
- Match the given string with regular expression. In Java, this can be done by using Pattern.matcher().
- Return true if the given string matched with the regular expression, else return false.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <regex>
using namespace std;
bool imageFile(string str)
{
const regex pattern( "[^\\s]+(.*?)\\.(jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF)$" );
if (str.empty())
{
return false ;
}
if (regex_match(str, pattern))
{
return true ;
}
else
{
return false ;
}
}
int main()
{
string str1 = "abc.png" ;
cout << imageFile(str1) << endl;
string str2 = "im.jpg" ;
cout << imageFile(str2) << endl;
string str3 = ".gif" ;
cout << imageFile(str3) << endl;
string str4 = "abc.mp3" ;
cout << imageFile(str4) << endl;
string str5 = " .jpg" ;
cout << imageFile(str5) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean imageFile(String str)
{
String regex
= "([^\\s]+(\\.(?i)(jpe?g|png|gif|bmp))$)" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "abc.png" ;
System.out.println(imageFile(str1));
String str2 = "im.jpg" ;
System.out.println(imageFile(str2));
String str3 = ".gif" ;
System.out.println(imageFile(str3));
String str4 = "abc.mp3" ;
System.out.println(imageFile(str4));
String str5 = " .jpg" ;
System.out.println(imageFile(str5));
}
}
|
Python3
import re
def imageFile( str ):
regex = "([^\\s]+(\\.(?i)(jpe?g|png|gif|bmp))$)"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "abc.png"
print (imageFile(str1))
str2 = "im.jpg"
print (imageFile(str2))
str3 = ".gif"
print (imageFile(str3))
str4 = "abc.mp3"
print (imageFile(str4))
str5 = " .jpg"
print (imageFile(str5))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static void Main( string [] args)
{
string [] str={ "abc.png" , "im.jpg" , ".gif" , "abc.mp3" , " .jpg" };
foreach ( string s in str) {
Console.WriteLine( imageFile(s) ? "true" : "false" );
}
Console.ReadKey(); }
public static bool imageFile( string str)
{
string strRegex = @"([^\s]+(\.(?i)(jpe?g|png|gif|bmp))$)" ;
Regex re = new Regex(strRegex);
if (re.IsMatch(str))
return ( true );
else
return ( false );
}
}
|
Javascript
function imageFile(str) {
let regex = new RegExp(/[^\s]+(.*?).(jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF)$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "abc.png" ;
console.log(imageFile(str1));
let str2 = "im.jpg" ;
console.log(imageFile(str2));
let str3 = ".gif" ;
console.log(imageFile(str3));
let str4 = "abc.mp3" ;
console.log(imageFile(str4));
let str5 = " .jpg" ;
console.log(imageFile(str5));
|
Output:
true
true
false
false
false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...