[英]how to split a String on date format
我的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
我該怎么做。
您也可以使用此正則表達式。
(?=\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
您可以嘗試如下操作:
str.split("(?=\\d{1,2}:\\d{1,2} (AM|PM))");
您可以使用此正則表達式進行拆分:
String[] arr = str.split("(?=(?<!\\d:)\b\\d{1,2}:\\d{1,2}(?::\\d{1,2})? [AP]M\b)\\s*");
在前行模式的兩側使用單詞邊界 \\b
是很重要的:
此正則表達式應創建所需的輸出。
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);
}
Java有一個稱為Matcher的類,您可以使用該類找到給定模式的出現。
這個問題在鏈接中有合適的解決方案: 使用正則表達式獲取字符串中模式的索引
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.