我有一個以下格式的字符串。通過java中的正則表達式計數和分割
-52/ABC/35/BY/200/L/DEF/307/C/110/L
我需要執行以下操作。
1. Find the no of occurrences of 3 letter word's like ABC,DEF in the above text.
2. Split the above string by ABC and DEF as shown below.
ABC/35/BY/200/L
DEF/307/C/110/L
我已經嘗試使用正則表達式與下面的代碼,但它始終顯示匹配計數爲零。如何輕鬆解決這個問題。
static String DEST_STRING = "^[A-Z]{3}$";
static Pattern DEST_PATTERN = Pattern.compile(DEST_STRING,
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
public static void main(String[] args) {
String test = "-52/ABC/35/BY/200/L/DEF/307/C/110/L";
Matcher destMatcher = DEST_PATTERN.matcher(test);
int destCount = 0;
while (destMatcher.find()) {
destCount++;
}
System.out.println(destCount);
}
請注意我需要使用JDK 6本,
你想要查找「每個」3個字母單詞的發生還是僅查找所有3個字母單詞的總數? – TheLostMind
另外,您的示例輸出是錯誤的。 DEF部分中沒有'XPS' – TheLostMind
想要找到「每個」3個字母詞出現的次數 – prabu