How to validate image file extension using Regular Expression | C ++ program to validate the image file extension using Regular Expression ; Function to validate the image file extension . ; Regex to check valid image file extension . ; If the image file extension is empty return false ; Return true if the image file extension matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE 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 ; }