[英]Java - How do i find out that a string is in the correct format using pattern matching
我的字符串值;
09:00-10:00,12:00-14:30,16:00-18:00
(這個字符串像這樣重復時間間隔n次)
我想使用模式匹配找出字符串的格式是否正確;
Pattern.matches("<Pattern Here>", stringValue);
是否可以?
我試過了;
Pattern.matches("^[0-9:0-9-0-9:0-9,]+$", value);
但不能正常工作
您可以檢查字符串是否與正則表達式^(?:(?:\d{2}:\d{2}\-\d{2}:\d{2})(?:,(??$))?)*$
。 請注意,在使用String#matches
時,您不需要以開頭(即^
)和結尾(即$
)。
如果字符串通過此驗證,則將字符串拆分為,
和-
然后使用 java.time API 來驗證各個時間字符串。
演示:
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(hasCorrectFormat("09:00-10:00,12:00-14:30,16:00-18:00")); // true
System.out.println(hasCorrectFormat("09:00-10:00,12:00-1:30,16:00-18:00")); // false -> 1:30 is not in desired
// format
System.out.println(hasCorrectFormat("09:00-10:00")); // true
System.out.println(hasCorrectFormat("09:00 10:00")); // false
System.out.println(hasCorrectFormat("09:00-10:00,09:00 10:00")); // false
System.out.println(hasCorrectFormat("09:00-10:00-12:00-14:30,16:00-18:00")); // false
System.out.println(hasCorrectFormat("09:00-10:00,12:00-14:30,16:00-18:00,")); // false
}
static boolean hasCorrectFormat(String strTimeRanges) {
if (!strTimeRanges.matches("(?:(?:\\d{2}:\\d{2}\\-\\d{2}:\\d{2})(?:,(?!$))?)*"))
return false;
String[] times = strTimeRanges.split("[-,]");
for (String time : times) {
try {
LocalTime.parse(time);
} catch (DateTimeParseException e) {
return false;
}
}
return true;
}
}
Output :
true
false
true
false
false
false
false
從Trail:Date Time了解有關現代日期時間 API 的更多信息。
Ole VV 的寶貴評論促使我擴展我的原始答案,以展示如何使用 java.time API 來比較各個時間范圍內的時間。 我希望這能給學習者足夠的提示,讓他們根據需求的復雜性進一步擴展解決方案。
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(hasCorrectFormatAndRanges("09:00-10:00,12:00-14:30,16:00-18:00")); // true
System.out.println(hasCorrectFormatAndRanges("09:00-10:00,12:00-1:30,16:00-18:00")); // false -> 1:30
System.out.println(hasCorrectFormatAndRanges("10:00-09:00,12:00-14:30,16:00-18:00")); // false -> 10:00-09:00
System.out.println(hasCorrectFormatAndRanges("09:00-10:00")); // true
System.out.println(hasCorrectFormatAndRanges("09:00 10:00")); // false
System.out.println(hasCorrectFormatAndRanges("09:00-10:00,09:00 10:00")); // false
System.out.println(hasCorrectFormatAndRanges("09:00-10:00-12:00-14:30,16:00-18:00")); // false
System.out.println(hasCorrectFormatAndRanges("09:00-10:00,12:00-14:30,16:00-18:00,")); // false
}
static boolean hasCorrectFormatAndRanges(String strTimeRanges) {
if (!strTimeRanges.matches("(?:(?:\\d{2}:\\d{2}\\-\\d{2}:\\d{2})(?:,(?!$))?)*"))
return false;
String[] timeRanges = strTimeRanges.split(",");
for (String timeRange : timeRanges) {
String[] times = timeRange.split("-");
try {
if (LocalTime.parse(times[1]).isBefore(LocalTime.parse(times[0])))
return false;
} catch (DateTimeParseException e) {
return false;
}
}
return true;
}
}
Output :
true
false
false
true
false
false
false
false
您可以以 24 小時時鍾的形式對任何給定的 2 次范圍使用以下模式:
private static final String HOURLY_TIME_PATTERN = "([01][0-9]|2[0-3]):([0-5][0-9])";
private static final String TIME_RANGER_PATTERN = HOURLY_TIME_PATTERN + "-" + HOURLY_TIME_PATTERN;
private static final String PATTERN = "^" + TIME_RANGER_PATTERN + "(," + TIME_RANGER_PATTERN + "?)*$";
private static final Pattern pattern = Pattern.compile(PATTERN);
public static void main(String[] args) {
String input = "09:00-10:00,12:00-14:30,16:00-18:00";
if (pattern.matcher(input).matches()) {
System.out.println("We have a match!");
}
}
解釋:
HOURLY_TIME_PATTERN
是 24 小時格式的一個小時所需的模式(例如 16:25)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.