2015-02-09 60 views
0

目前,我有以下代碼:的Java分割字符串得到的只有字母(但不是字母或數字)

String test = "=(7+A15)-5"; 
if(test.matches(".*[A-z]+[0-9]+.*")){ 
    String spl[] = test.split("((?<=[A-z]{1,4}[0-9]{1,4})|(?=[A-z]{1,4}[0-9]{1,4}))",3); 
    System.out.println(spl[0] + "\n" + spl[1] + "\n" +spl[2]);    
} 

它打印我:

=(7+ 
A1 
5)-5 

除此之外,我想:

=(7+ 
A15 
)-5 

但我不知道爲什麼它只有一個數字,當我問{1,4}

+2

'[AZ]'包括字符' '[', '\\', ']', '^',「_ ','和''''''。在'new String(「foo」)'中包裝一個字符串沒有意義。 – 2015-02-09 18:44:31

+0

字母數字令牌的範圍是什麼(從您的正則表達式看來,您正在從1到4 {1,4}每個Alpha和{1,4}每個數字。ABCD1234有效,但ABCDEF12345 NOT? – gtgaxiola 2015-02-09 19:26:28

+0

正確,ABCD1234是有效的,而ABCDEF ...不是 – Naouk 2015-02-09 19:27:19

回答

0

我會用一個分隔符,如

String delimiter = "[a-zA-Z]{1,4}[0-9]{1,4}"; 

然後以捕捉的分隔符,以及你要拆分的令牌兩者都做一個lookaheadlookbehind

//Lookahead and lookbehind where %1 is the delimiter 
String WITH_DELIMITER = "((?<=%1$s)|(?=%1$s))"; 
String regex = String.format(WITH_DELIMITER, delimiter); 

因爲它似乎是捕獲組拆分正則表達式匹配的那一刻

例子:A15會分成A15

您需要遍歷您的令牌來連接這些令牌實際上是分隔符(使用不同的匹配器)

String matcher = "[a-zA-Z]*[0-9]+"; 
String[] s = text.split(regex); 
List<String> result = new ArrayList<>(); 
String tmp = ""; 
for (String x : s) { 
    if (x.matches(matcher)) { 
     tmp += x; 
    } else { 
     if (!tmp.isEmpty()) { 
      result.add(tmp); 
      tmp = ""; 
     } 
     result.add(x); 
    } 
} 
if(!tmp.isEmpty()) { 
    result.add(tmp); 
} 

全部放在一起:

public static List<String> splitWithDelimiter(String text, String delimiter) { 
    String WITH_DELIMITER = "((?<=%1$s)|(?=%1$s))"; 
    String regex = String.format(WITH_DELIMITER, delimiter); 
    String matcher = "[a-zA-Z]*[0-9]+"; 
    String[] s = text.split(regex); 
    List<String> result = new ArrayList<>(); 
    String tmp = ""; 
    for (String x : s) { 
     if (x.matches(matcher)) { 
      tmp += x; 
     } else { 
      if (!tmp.isEmpty()) { 
       result.add(tmp); 
       tmp = ""; 
      } 
      result.add(x); 
     } 
    } 
    if(!tmp.isEmpty()) { 
     result.add(tmp); 
    } 
    return result; 
} 

public static void main(String[] args) { 
    String test = "=(7+A185)-5"; 
    String delimiter = "[a-zA-Z]{1,4}[0-9]{1,4}"; 
    List<String> s = splitWithDelimiter(test, delimiter); 
    for (String x : s) { 
     System.out.println(x); 
    } 
} 

,其輸出:

=(7+ 
A185 
)-5 
相關問題