给定字符串str ,任务是使用正则表达式检查给定字符串是否为有效的GUID(全局唯一标识符)。
有效的GUID(全局唯一标识符)必须指定以下条件:
- 它应该是一个128位数字。
- 长度应为36个字符(32个十六进制字符和4个连字符)。
- 它应显示为五组,并用连字符(-)分隔。
- Microsoft GUID有时用大括号表示。
例子:
Input: str = “123e4567-e89b-12d3-a456-9AC7CBDCEE52”
Output: true
Explanation:
The given string satisfies all the above mentioned conditions. Therefore, it is a valid GUID (Globally Unique Identifier).
Input: str = “123e4567-h89b-12d3-a456-9AC7CBDCEE52”
Output: false
Explanation:
The given string contains ‘h’, the valid hexadecimal characters should be followed by character from a-f, A-F, and 0-9. Therefore, it is not a valid GUID (Globally Unique Identifier).
Input: str = “123e4567-h89b-12d3-a456”
Output: false
Explanation:
The given string has 20 characters. Therefore, it is not a valid GUID (Globally Unique Identifier).
方法:想法是使用正则表达式解决此问题。可以按照以下步骤计算答案:
- 获取字符串。
- 创建一个正则表达式以检查有效的GUID(全局唯一标识符),如下所述:
regex = “^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$”
- 在哪里:
- ^表示字符串的开头。
- [{]?代表可选的'{‘字符。
- [0-9a-fA-F] {8}代表来自AF,AF和0-9的8个字符。
- –表示连字符。
- ([0-9a-fA-F] {4}-){3}代表af,AF和0-9中的4个字符,该字符重复3次,并以连字符(-)分隔。
- [0-9a-fA-F] {12}代表af,AF和0-9中的12个字符。
- [}]?表示可选的’}’字符。
- $表示字符串的结尾。
- 将给定的字符串与正则表达式匹配。在Java,这可以通过使用Pattern.matcher()来完成。
- 如果字符串与给定的正则表达式匹配,则返回true;否则返回false。
下面是上述方法的实现:
C++
// C++ program to validate the
// GUID (Globally Unique Identifier) using Regular Expression
#include
#include
using namespace std;
// Function to validate the GUID (Globally Unique Identifier).
bool isValidGUID(string str)
{
// Regex to check valid GUID (Globally Unique Identifier).
const regex pattern("^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$");
// If the GUID (Globally Unique Identifier)
// is empty return false
if (str.empty())
{
return false;
}
// Return true if the GUID (Globally Unique Identifier)
// matched the ReGex
if(regex_match(str, pattern))
{
return true;
}
else
{
return false;
}
}
// Driver Code
int main()
{
// Test Case 1:
string str1 = "123e4567-e89b-12d3-a456-9AC7CBDCEE52";
cout << isValidGUID(str1) << endl;
// Test Case 2:
string str2 = "{123e4567-e89b-12d3-a456-9AC7CBDCEE52}";
cout << isValidGUID(str2) << endl;
// Test Case 3:
string str3 = "123e4567-h89b-12d3-a456-9AC7CBDCEE52";
cout << isValidGUID(str3) << endl;
// Test Case 4:
string str4 = "123e4567-h89b-12d3-a456";
cout << isValidGUID(str4) << endl;
return 0;
}
// This code is contributed by yuvraj_chandra
Java
// Java program to validate
// GUID (Globally Unique Identifier)
// using regular expression
import java.util.regex.*;
class GFG {
// Function to validate
// GUID (Globally Unique Identifier)
// using regular expression
public static boolean
isValidGUID(String str)
{
// Regex to check valid
// GUID (Globally Unique Identifier)
String regex
= "^[{]?[0-9a-fA-F]{8}"
+ "-([0-9a-fA-F]{4}-)"
+ "{3}[0-9a-fA-F]{12}[}]?$";
// Compile the ReGex
Pattern p = Pattern.compile(regex);
// If the string is empty
// return false
if (str == null) {
return false;
}
// Find match between given string
// and regular expression
// uSing Pattern.matcher()
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 str2
= "123e4567-e89b-12d3"
+ "-a456-9AC7CBDCEE52";
System.out.println(
isValidGUID(str2));
// Test Case 2:
String str3
= "{123e4567-e89b-12d3-"
+ "a456-9AC7CBDCEE52}";
System.out.println(
isValidGUID(str3));
// Test Case 3:
String str1
= "123e4567-h89b-12d3-a456"
+ "-9AC7CBDCEE52";
System.out.println(
isValidGUID(str1));
// Test Case 4:
String str4
= "123e4567-h89b-12d3-a456";
System.out.println(
isValidGUID(str4));
}
}
Python3
# Python3 program to validate
# GUID (Globally Unique Identifier)
# using regular expression
import re
# Function to validate GUID
# (Globally Unique Identifier)
def isValidGUID(str):
# Regex to check valid
# GUID (Globally Unique Identifier)
regex = "^[{]?[0-9a-fA-F]{8}" + "-([0-9a-fA-F]{4}-)" + "{3}[0-9a-fA-F]{12}[}]?$"
# 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 = "123e4567-e89b-12d3" + "-a456-9AC7CBDCEE52"
print(isValidGUID(str1))
# Test Case 2:
str2 = "{123e4567-e89b-12d3-" + "a456-9AC7CBDCEE52}"
print(isValidGUID(str2))
# Test Case 3:
str3 = "123e4567-h89b-12d3-a456" + "-9AC7CBDCEE52"
print(isValidGUID(str3))
# Test Case 4:
str4 = "123e4567-h89b-12d3-a456"
print(isValidGUID(str4))
# This code is contributed by avanitrachhadiya2155
true
true
false
false