2017-02-17 37 views
0

我有一個以下格式的字符串。通過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本,

+0

你想要查找「每個」3個字母單詞的發生還是僅查找所有3個字母單詞的總數? – TheLostMind

+0

另外,您的示例輸出是錯誤的。 DEF部分中沒有'XPS' – TheLostMind

+0

想要找到「每個」3個字母詞出現的次數 – prabu

回答

2

您可以使用此代碼:

public static void main(String[] args) throws Exception { 
    String s = "-52/ABC/35/BY/200/L/DEF/307/C/110/L"; 
    // Pattern to find all 3 letter words . The \\b means "word boundary", which ensures that the words are of length 3 only. 
    Pattern p = Pattern.compile("(\\b[a-zA-Z]{3}\\b)"); 
    Matcher m = p.matcher(s); 
    Map<String, Integer> countMap = new HashMap<>(); 
    // COunt how many times each 3 letter word is used. 
    // Find each 3 letter word. 
    while (m.find()) { 
     // Get the 3 letter word. 
     String val = m.group(); 
     // If the word is present in the map, get old count and add 1, else add new entry in map and set count to 1 
     if (countMap.containsKey(val)) { 
      countMap.put(val, countMap.get(val) + 1); 
     } else { 
      countMap.put(val, 1); 
     } 
    } 
    System.out.println(countMap); 
    // Get ABC.. and DEF.. using positive lookahead for a 3 letter word or end of String 
    // Finds and selects everything starting from a 3 letter word until another 3 letter word is found or until string end is found. 
    p = Pattern.compile("(\\b[a-zA-Z]{3}\\b.*?)(?=/[A-Za-z]{3}|$)"); 
    m = p.matcher(s); 
    while (m.find()) { 
     String val = m.group(); 
     System.out.println(val); 
    } 

} 

O/P:

{ABC=1, DEF=1} 
ABC/35/BY/200/L 
DEF/307/C/110/L 
+0

它按預期工作。你能否解釋一下你的解決方案? – prabu

+1

@prabu - 在答案中增加了額外的評論。 – TheLostMind

+0

我想學習這些複雜的正則表達式..你能請指導我一些最好的教程或適合新手的網站? – prabu

1

檢查這一項:

String stringToSearch = "-52/ABC/35/BY/200/L/DEF/307/C/110/L"; 
Pattern p1 = Pattern.compile("\\b[a-zA-Z]{3}\\b"); 
Matcher m = p1.matcher(stringToSearch); 

int startIndex = -1; 
while (m.find()) 
{ 
    //Try to use Apache Commons' StringUtils 
    int count = StringUtils.countMatches(stringToSearch, m.group()); 
    System.out.println(m.group +":"+ count); 

    if(startIndex != -1){ 
     System.out.println(stringToSearch.substring(startIndex,m.start()-1)); 
    } 
    startIndex = m.start(); 
} 
if(startIndex != -1){ 
    System.out.println(stringToSearch.substring(startIndex)); 
} 

輸出:

ABC:1

ABC/35/BY/200/L

DEF:1

DEF/307/C/110/L