How to validate image file extension using Regular Expression

Given a string str, the task is to check whether the given string is valid image file extension or not by using Regular Expression.

The valid image file extension must specify the following conditions:

  1. It should start with a string of atleast one character.
  2. It should not have any white space.
  3. It should be followed by a dot(.).
  4. 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 atleast one character). Therefore it is not a valid image file extension.

Approach: This problem can be solved by using regular expression.

  1. Get the String.
  2. Create a regular expression to check valid image file extension as mentioned below:

    regex = “([^\\s]+(\\.(?i)(jpe?g|png|gif|bmp))$)”;

    Where:

    • ( represents the starting of the group 1.
    • [^\\s]+ represents the string must contains atleast one character.
    • ( represents the starting of the group 2.
    • \\. represents the string should follow by a dot(.).
    • (?i) represents the string ignore the case sensitive.
    • ( represents the starting of the 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.
  3. Match the given string with regular expression. In Java, this can be done by using Pattern.matcher().
  4. Return true if the given string matched with the regular expression, else return false.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check valid
// image file extension using regex
  
import java.util.regex.*;
class GFG {
  
    // Function to validate image file extension .
    public static boolean imageFile(String str)
    {
        // Regex to check valid image file extension.
        String regex
            = "([^\\s]+(\\.(?i)(jpe?g|png|gif|bmp))$)";
  
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
  
        // If the string is empty
        // return false
        if (str == null) {
            return false;
        }
  
        // Pattern class contains matcher() method
        // to find matching between given string
        // and regular expression.
        Matcher m = p.matcher(str);
  
        // Return if the string
        // matched the ReGex
        return m.matches();
    }
  
    // Driver code
    public static void main(String args[])
    {
  
        // Test Case 1:
        String str1 = "abc.png";
        System.out.println(imageFile(str1));
  
        // Test Case 2:
        String str2 = "im.jpg";
        System.out.println(imageFile(str2));
  
        // Test Case 3:
        String str3 = ".gif";
        System.out.println(imageFile(str3));
  
        // Test Case 4:
        String str4 = "abc.mp3";
        System.out.println(imageFile(str4));
  
        // Test Case 5:
        String str5 = " .jpg";
        System.out.println(imageFile(str5));
    }
}

chevron_right


Output:

true
true
false
false
false

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.