Valid file extension checker using Regular Expression
Given string str, the task is to check whether the given string is a valid file extension or not by using Regular Expression.
The valid 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 end with any one of the file extensions:
ex : jpg, txt, mp3, png, ppt, .. etc
Examples:
Input: str = document.txt
Output: text
Explanation: Here the string extends .txt then it is a text file.Input: str = picture.jpg
Output: picture
Explanation: Here the string extends .jpg then it is a picture file.
Below are the steps involved in the implementation of the code:
- Get the String.
- Converted the string to lowercase using the lower method, in case it is in uppercase.
- Extract the extension of the substring starting from the position of the ‘.’ character to the end of the string.
- If the extension matches one of the known extensions, the function returns a string indicating the type of the file (e.g. ‘image’ for image files, ‘audio’ for audio files, etc.). If the extension doesn’t match any of the known extensions, the function returns ‘unknown’.
- print the file name.
Below is the implementation of the above approach:
C++
// C++ code for the above approach: #include <bits/stdc++.h> using namespace std; string get_file_type(string filename) { // Convert the string to lowercase transform(filename.begin(), filename.end(), filename.begin(), ::tolower); // Get the substring after (.) string ext = filename.substr(filename.find_last_of(".")); if (ext == ".txt" || ext == ".csv" || ext == ".tsv") { return "text"; } else if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif") { return "image"; } else if (ext == ".pdf") { return "pdf"; } else if (ext == ".mp3" || ext == ".wav" || ext == ".wma") { return "audio"; } else if (ext == ".mp4" || ext == ".avi" || ext == ".mov" || ext == ".wmv") { return "video"; } else if (ext == ".doc" || ext == ".docx") { return "document"; } else if (ext == ".xls" || ext == ".xlsx") { return "spreadsheet"; } else if (ext == ".ppt" || ext == ".pptx") { return "presentation"; } else if (ext == ".zip" || ext == ".rar" || ext == ".7z") { return "archive"; } else if (ext == ".exe" || ext == ".msi") { return "executable"; } else if (ext == ".java" || ext == ".c" || ext == ".py" || ext == ".cpp") { return "code"; } else { return "unknown"; } } // Driver code int main() { string str = "document.txt"; // Function call string ans = get_file_type(str); cout << ans << endl; return 1; }
Time Complexity: O(1)
Auxiliary Space: O(1)
Related Articles:
Given string str, the task is to check whether the given string is a valid file extension or not by using Regular Expression.
The valid 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 end with any one of the file extensions:
ex : jpg, txt, mp3, png, ppt, .. etc
Examples:
Input: str = document.txt
Output: text
Explanation: Here the string extends .txt then it is a text file.Input: str = picture.jpg
Output: picture
Explanation: Here the string extends .jpg then it is a picture file.
Below are the steps involved in the implementation of the code:
- Get the String.
- Converted the string to lowercase using the lower method, in case it is in uppercase.
- Extract the extension of the substring starting from the position of the ‘.’ character to the end of the string.
- If the extension matches one of the known extensions, the function returns a string indicating the type of the file (e.g. ‘image’ for image files, ‘audio’ for audio files, etc.). If the extension doesn’t match any of the known extensions, the function returns ‘unknown’.
- print the file name.
Below is the implementation of the above approach:
C++
// C++ code for the above approach: #include <bits/stdc++.h> using namespace std; string get_file_type(string filename) { // Convert the string to lowercase transform(filename.begin(), filename.end(), filename.begin(), ::tolower); // Get the substring after (.) string ext = filename.substr(filename.find_last_of(".")); if (ext == ".txt" || ext == ".csv" || ext == ".tsv") { return "text"; } else if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif") { return "image"; } else if (ext == ".pdf") { return "pdf"; } else if (ext == ".mp3" || ext == ".wav" || ext == ".wma") { return "audio"; } else if (ext == ".mp4" || ext == ".avi" || ext == ".mov" || ext == ".wmv") { return "video"; } else if (ext == ".doc" || ext == ".docx") { return "document"; } else if (ext == ".xls" || ext == ".xlsx") { return "spreadsheet"; } else if (ext == ".ppt" || ext == ".pptx") { return "presentation"; } else if (ext == ".zip" || ext == ".rar" || ext == ".7z") { return "archive"; } else if (ext == ".exe" || ext == ".msi") { return "executable"; } else if (ext == ".java" || ext == ".c" || ext == ".py" || ext == ".cpp") { return "code"; } else { return "unknown"; } } // Driver code int main() { string str = "document.txt"; // Function call string ans = get_file_type(str); cout << ans << endl; return 1; }
Time Complexity: O(1)
Auxiliary Space: O(1)
Related Articles: