gpt4 book ai didi

java - 用于搜索特定关键字的正则表达式以及用于时间搜索的正则表达式

转载 作者:行者123 更新时间:2023-12-02 01:32:56 25 4
gpt4 key购买 nike

我有一个 java 字符串,其中包含时间(am|pm 格式)以及特定关键字(如 SET 或 UNSET)以及其他一些不相关的单词。例如将时间设置为上午 10:30或将时间取消设置为晚上 10:30。

我已经有了时间的正则表达式

regex_am_pm = "(?:\\s{1,2}[1-9]|\\s{1,2}0[1-9]|\\s{1,2}1[0-2]):[0-5][0-9]\\s{0,2}(?:am|pm|AM|PM)";.

如何包含正则表达式来检查 REGEX 时间之前的缩写 SET 或 UNSET。

请帮忙。

regex_am_pm = "(SET|UNSET)(?:\\s{1,2}[1-9]|\\s{1,2}0[1-9]|\\s{1,2}1[0-2]):[0-5][0-9]\\s{0,2}(?:am|pm|AM|PM)";

我想使用时间正则表达式搜索设置或取消设置。之间允许的任何内容

预期输出

String passed = "hey Set clock to 10:30 PM"
if SET found before time regex
outPut if(SetMethod(String time))
else(UNSetMethod(String time))

最佳答案

我认为您希望验证传入的字符串,以确保它实际上包含单词 SET 或 UNSET(无论字母大小写),单词 time 在字符串内,以便确认 SET 的用途,以及字符串中还包含时间的事实。

字符串规则:

  • 必须包含单词SETUNSET(无论字母如何)案件)。两者都必须是独立的单词,而不是另一个单词的一部分;
  • 必须在某处包含单词时间(无论字母大小写) 在单词SETUNSET之后,以消除一些含糊不清;
  • 必须包含格式为 hh:mm 的时间(12 小时时间)或HH:mm(24 小时时间)。 AMPM 是可选的,大小写无关。实际时间必须放在之后的某个位置再次使用时间一词,以消除一些歧义。

您可以尝试的一些代码:

// The Regular Expression (RegEx) we are going to use...
String regEx = "(?i)(\\bSET\\b|\\bUNSET\\b)(.*?\\btime\\b.*?)?(\\d{2}\\:\\d{2}(\\s+)?(am|pm)?)";

String incomingString = "set time as 10:30 am";

String setType = "NONE AVAILABLE!"; // Default
String setTime = "NONE AVAILABLE!"; // Default
String timeFormat = ""; // Default

// Does the incoming String meet our requirements?
if (incomingString.trim().matches(regEx)) {
// Yes it does...
System.out.println("String contains valid content.");

// Get the required information from the input String...
Pattern r = Pattern.compile(regEx);
Matcher m = r.matcher(incomingString);
if (m.find()) {
setType = m.group(1).toUpperCase();
setTime = m.group(3).toUpperCase();
}

// Is the time Valid
timeFormat = validateTime(setTime); // see validateTime() method
if (timeFormat.equals("NONE")) {
// Reset to defaults
setType = "NONE AVAILABLE!";
setTime = "NONE AVAILABLE!";
}
}
// Display the findings...
System.out.println("Set Type: " + setType);
System.out.println("Time: " + setTime + " (in " + timeFormat + ")");

validateTime() 方法:

/**
* If valid this method will return a string indicating the Time Format
* otherwise it will return the uppercase word string: "NONE".<br>
*
* @param time (String) The time to validate in HH:mm or hh:mm (am/pm).<br>
*
* @return (String) Either "24 Hour Format", "12 Hour Format", or "NONE" if
* validation fails.
*/
public static String validateTime(String time) {
String fmt = "NONE";
// 12 Hour Time...
if (time.matches("(1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm)")) {
fmt = "12 Hour Format";
}
// 24 Hour Time...
else if (time.matches("([01]?[0-9]|2[0-3]):[0-5][0-9]")) {
fmt = "24 Hour Format";
}
return fmt;
}

正则表达式说明:

(?i)(\\bSET\\b|\\bUNSET\\b)(.*?\\btime\\b.*?)?(\d{2}\\:\\d{2}(\\s+)?(am|pm)?)

(?i)
match the remainder of the pattern with the following effective flags: gmi
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

1st Capturing Group: (\\bSET\\b|\\bUNSET\\b)
1st Alternative: \\bSET\\b
\\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
SET matches the characters SET literally (case insensitive)
\\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)

2nd Alternative: \\bUNSET\\b
\\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
UNSET matches the characters UNSET literally (case insensitive)
\\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)

2nd Capturing Group: (.*?\\btime\\b.*?)?
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
.*? matches any character (except for line terminators)
*? Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed (lazy)
\\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
time matches the characters time literally (case insensitive)
\\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
.*? matches any character (except for line terminators)

3rd Capturing Group: (\\d{2}\\:\\d{2}(\\s+)?(am|pm)?)
\\d{2} matches a digit (equal to [0-9])
{2} Quantifier — Matches exactly 2 times
\\: matches the character : literally (case insensitive)
\\d{2} matches a digit (equal to [0-9])
{2} Quantifier — Matches exactly 2 times

4th Capturing Group: (\\s+)?
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
\s+ matches any whitespace character (equal to [\r\n\t\f\v ])

5th Capturing Group: (am|pm)?
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative: am
am matches the characters am literally (case insensitive)
2nd Alternative: pm
pm matches the characters pm literally (case insensitive)

关于java - 用于搜索特定关键字的正则表达式以及用于时间搜索的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57545475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com