2017-12-27 201 views
2

可能與問題3100585模糊相關。自定義類不會過濾掉行尾字符或空白

以下類的目的是獲取包含一行Java源代碼的字符串,並將其劃分爲將由單獨的類進一步分析的令牌字符串。 split方法中顯示的正則表達式將字符串除以運算符字符和空白,保留所有字符,然後該類遍歷結果數組並刪除所有空白字符和行尾字符。然後它將數組轉換成一個ArrayList並返回它。

public class Lexer { 

Lexer() { 
} 

public List<String> convertStringToTokens(String input) { 
    input = input.trim(); 

    String[] result = input.split("(?<=[-+*\/=\s\<\>\(\)])|(?=[-+*\/=\s\<\>\(\)])"); 
    List<String> resultList = new LinkedList<>(Arrays.asList(result)); 

    for (Iterator<String> iterator = resultList.iterator(); iterator.hasNext();) { 
     String string = iterator.next(); 
     if (string.isEmpty() || string.matches("\\u000A") || string.matches("\\u000D") || string.matches(" ") || string.matches("\\u000B")) { 

       iterator.remove(); 
      } 
     } 

     return resultList; 
    } 
} 

不幸的是,班級沒有履行預期的角色,我不確定的原因。

最有可能的正則表達式在這裏是錯誤的。

如果有人知道我在這發生了什麼問題,請通知並告知。

編輯:輸入是單個字符串,如「Sphere s = new Sphere(16);」。輸出是一個字符串的ArrayList,(最多)長度爲兩個字符串,其中對於上述輸入將是

{"Sphere s = new Sphere(16",");"}. 

(右括號的分離形成參數意圖。順便說一下,會有人知道如何將參數與左括號分開?)

+0

你能提供樣品輸入如你預期它是不工作的輸出? – murraybo

回答

0

我找到了一個解決方案:只是將空間匹配從lookaround斷言(在提問後添加)移動到單獨的替代匹配,允許我刪除空格字符和將字符串也分開。

String[] result = input.split("(?<=[ -+*\/=\s\<\>\(\)])|(?=[ -+*\/=\s\<\>\(\)])"); 

成爲

String[] result = input.split("(?<=[-+*\/=\s\<\>\(\)])|(?=[-+*\/=\s\<\>\(\)])| ");