给定字符串str ,任务是使用正则表达式检查给定字符串是否为有效的图像文件扩展名。
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.
例子:
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.
方法:可以使用正则表达式解决此问题。
- 获取字符串。
- 创建一个正则表达式以检查有效的图像文件扩展名,如下所示:
regex = “([^\\s]+(\\.(?i)(jpe?g|png|gif|bmp))$)”;
- 在哪里:
- (代表第1组的开始。
- [^ \\ s] +表示字符串必须包含至少一个字符。
- (代表第2组的开始。
- \\。表示字符串,后跟一个点号(。)。
- (?i)表示字符串,忽略大小写。
- (代表第3组的开始。
- jpe?g | png | gif | bmp表示以jpg或jpeg或png或gif或bmp扩展名的字符串结尾。
- )代表群组3的结尾。
- )代表群组2的结尾。
- $代表字符串。
- )代表第1组的结尾。
- 将给定的字符串与正则表达式匹配。在Java,这可以通过使用Pattern.matcher()来完成。
- 如果给定的字符串与正则表达式匹配,则返回true,否则返回false。
下面是上述方法的实现:
C++
// C++ program to validate the
// image file extension using Regular Expression
#include
#include
using namespace std;
// Function to validate the image file extension.
bool imageFile(string str)
{
// Regex to check valid image file extension.
const regex pattern("[^\\s]+(.*?)\\.(jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF)$");
// If the image file extension
// is empty return false
if (str.empty())
{
return false;
}
// Return true if the image file extension
// matched the ReGex
if(regex_match(str, pattern))
{
return true;
}
else
{
return false;
}
}
// Driver Code
int main()
{
// Test Case 1:
string str1 = "abc.png";
cout << imageFile(str1) << endl;
// Test Case 2:
string str2 = "im.jpg";
cout << imageFile(str2) << endl;
// Test Case 3:
string str3 = ".gif";
cout << imageFile(str3) << endl;
// Test Case 4:
string str4 = "abc.mp3";
cout << imageFile(str4) << endl;
// Test Case 5:
string str5 = " .jpg";
cout << imageFile(str5) << endl;
return 0;
}
// This code is contributed by yuvraj_chandra
Java
// 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));
}
}
Python3
# Python3 program to validate
# image file extension using regex
import re
# Function to validate
# image file extension .
def imageFile(str):
# Regex to check valid image file extension.
regex = "([^\\s]+(\\.(?i)(jpe?g|png|gif|bmp))$)"
# Compile the ReGex
p = re.compile(regex)
# If the string is empty
# return false
if (str == None):
return False
# Return if the string
# matched the ReGex
if(re.search(p, str)):
return True
else:
return False
# Driver code
# Test Case 1:
str1 = "abc.png"
print(imageFile(str1))
# Test Case 2:
str2 = "im.jpg"
print(imageFile(str2))
# Test Case 3:
str3 = ".gif"
print(imageFile(str3))
# Test Case 4:
str4 = "abc.mp3"
print(imageFile(str4))
# Test Case 5:
str5 = " .jpg"
print(imageFile(str5))
# This code is contributed by avanitrachhadiya2155
输出:
true
true
false
false
false