给定字符串str ,任务是使用正则表达式检查字符串是否为12小时格式的有效时间。
12小时格式的有效时间必须满足以下条件:
- 它应从1、2,…9或10、11、12开始。
- 后面应跟一个冒号(:)。
- 在其后应是两位数,介于00到59之间。
- 它应该只允许一个空格,尽管这是可选的。
- 它应以“ am”,“ pm”或“ AM”,“ PM”结尾。
例子:
Input: str = 12:15 AM
Output: true
Explanation: The given string satisfies all the above mentioned conditions.
Input: str = 9:45PM
Output: true
Explanation: The given string satisfies all the above mentioned conditions.
Input: str = 1:15
Output: false
Explanation: The given string does not end with ‘AM’ or ‘PM’, therefore it is not a valid time in 12-hour format.
Input: str = 17:30 PM
Output: false
Explanation: The given string does not have hours in between 1 to 12, therefore it is not a valid time in 12-hour format.
方法:可以使用正则表达式解决此问题。
- 获取字符串。
- 创建一个正则表达式以12小时格式检查时间,如下所述:
regex = "(1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm)";
- 在哪里:
- (代表群组的开始。
- 1 [012] | [1-9]代表以10、11、12或1、2 …开头的时间。 9
- )代表组的结尾。
- :表示时间后必须跟冒号(:)。
- [0-5] [0-9]表示时间,后跟00至59。
- (\\ s)?代表空白,时间后面是空白,这是可选的。
- (?i)表示不区分大小写,“ am”,“ pm”或“ AM”,“ PM”分别相同。
- (am | pm)表示时间应以“ am”,“ pm”或“ AM”,“ PM”结尾。
- 将给定的字符串与正则表达式匹配,在Java,这可以通过使用Pattern.matcher()来完成。
- 如果字符串与给定的正则表达式匹配,则返回true;否则返回false。
下面是上述方法的实现:
Java
// Java program to validate the time in
// 12-hour format using Regular Expression.
import java.util.regex.*;
class GFG {
// Function to validate the time in 12-hour format.
public static boolean isValidTime(String time)
{
// Regex to check valid time in 12-hour format.
String regexPattern
= "(1[012]|[1-9]):"
+ "[0-5][0-9](\\s)"
+ "?(?i)(am|pm)";
// Compile the ReGex
Pattern compiledPattern
= Pattern.compile(regexPattern);
// If the time is empty
// return false
if (time == null) {
return false;
}
// Pattern class contains matcher() method
// to find matching between given time
// and regular expression.
Matcher m = compiledPattern.matcher(time);
// Return if the time
// matched the ReGex
return m.matches();
}
// Driver Code.
public static void main(String args[])
{
// Test Case 1:
String str1 = "12:15 AM";
System.out.println(isValidTime(str1));
// Test Case 2:
String str2 = "9:45PM";
System.out.println(isValidTime(str2));
// Test Case 3:
String str3 = "1:15";
System.out.println(isValidTime(str3));
// Test Case 4:
String str4 = "17:30";
System.out.println(isValidTime(str4));
}
}
Python3
# Python3 program to validate the time in
# 12-hour format using Regular Expression.
import re
# Function to validate the time in 12-hour format.
def isValidTime(time) :
# Regex to check valid time in 12-hour format.
regexPattern = "(1[012]|[1-9]):"+ "[0-5][0-9](\\s)"+ "?(?i)(am|pm)";
# Compile the ReGex
compiledPattern = re.compile(regexPattern);
# If the time is empty
# return false
if (time == None) :
return False;
# re library contains search() method
# to find matching between given time
# and regular expression.
# Return if the time
# matched the ReGex
if re.search(compiledPattern,time):
return True
else :
return False
# Driver Code.
if __name__ == "__main__" :
# Test Case 1:
str1 = "12:15 AM";
print(isValidTime(str1));
# Test Case 2:
str2 = "9:45PM";
print(isValidTime(str2));
# Test Case 3:
str3 = "1:15";
print(isValidTime(str3));
# Test Case 4:
str4 = "17:30";
print(isValidTime(str4));
# This code is contributed by AnkitRai01
C++
// C++ program to validate
// time in 12-hour format
// using Regular Expression
#include
#include
using namespace std;
// Function to validate time in 12-hour format
bool isValidTime(string str)
{
// Regex to check valid time in 12-hour format
const regex pattern(
"((([1-9])|(1[0-2])):([0-5])([0-9])(\\s)(A|P)M)");
// If the time in 12-hour format
// is empty return false
if (str.empty())
{
return false;
}
// Return true if the time in 12-hour format
// matched the ReGex
if (regex_match(str, pattern))
{
return true;
}
else
{
return false;
}
}
// Driver Code
int main()
{
// Test Case 1:
string str1 = "12:15 AM";
cout << isValidTime(str1) << endl;
// Test Case 2:
string str2 = "9:45 PM";
cout << isValidTime(str2) << endl;
// Test Case 3:
string str3 = "1:15";
cout << isValidTime(str3) << endl;
// Test Case 4:
string str4 = "17:30";
cout << isValidTime(str4) << endl;
return 0;
}
// This code is contributed by yuvraj_chandra
输出
true
true
false
false