我的java类中有一个以下字符串值。
Nazar 1:46 PM Hello, I have few questions related to API Management 1:46 PM Sadhana has joined
我想根据其中提到的时间拆分它。它应该以这样的方式显示输出:每当有时间值到来时,它就会创建一个新的换行符。如下所示:
Nazar
1:46 PM Hello, I have few questions related to API Management.
1:46 PM Sadhana has joined
我怎样才能做到这一点。
答案 0 :(得分:2)
您也可以使用此正则表达式。
(?=\b(((11|12|10|(0[1-9]{1})):)|[\d]:)[0-5][0-9](\s)?(?i)(am|pm))
Descritpion:
它应与hh:mm AM/PM
和h:mm AM/PM
匹配。
hh -> hours 01-09 or 10,11,12 or 1-9
mm -> minutes 00-59
AM/PM -> case insensitive
用法:
String text = "Nazar 11:46 PM Hello, I have few questions related to API Management 10:46 PM "
+ "Sadhana has joined 06:35 AM its early 6:50 PM and late";
String texts[] = text.split("(?=\\b(((11|12|10|(0[1-9]{1})):)|[\\d]:)[0-5][0-9](\\s)?(?i)(am|pm))");
for(String s : texts){
System.out.println(s);
}
输出:
Nazar
11:46 PM Hello, I have few questions related to API Management
10:46 PM Sadhana has joined
06:35 AM its early
6:50 PM and late
答案 1 :(得分:1)
您可以尝试这样的事情:
str.split("(?=\\d{1,2}:\\d{1,2} (AM|PM))");
答案 2 :(得分:1)
您可以使用此正则表达式进行拆分:
<?php
$xml = $argv[1];
$dom = new DOMDocument();
// Initial block (must before load xml string)
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
// End initial block
$dom->loadXML($xml);
$out = $dom->saveXML();
print_R($out);
在前瞻模式的任意一侧使用字边界 String[] arr = str.split("(?=(?<!\\d:)\b\\d{1,2}:\\d{1,2}(?::\\d{1,2})? [AP]M\b)\\s*");
非常重要:
答案 3 :(得分:0)
这个正则表达式应该创建你想要的输出。
String s = "Nazar 1:46 PM Hello, I have few questions related to API Management 10:46 PM Sadhana has joined";
String s1[] = s.split("(\\s)(?=([01]?\\d:[0-6]?\\d\\s?([Aa]|[Pp])[Mm]))");
for (String s2 : s1) {
System.out.println(s2);
}
答案 4 :(得分:0)
Java有一个名为Matcher的类,您可以使用它来查找给定模式的出现次数。
此问题在链接中有合适的解决方案:Get the index of a pattern in a string using regex