我會用一個分隔符,如
String delimiter = "[a-zA-Z]{1,4}[0-9]{1,4}";
然後以捕捉的分隔符,以及你要拆分的令牌兩者都做一個lookahead
和lookbehind
。
//Lookahead and lookbehind where %1 is the delimiter
String WITH_DELIMITER = "((?<=%1$s)|(?=%1$s))";
String regex = String.format(WITH_DELIMITER, delimiter);
因爲它似乎是捕獲組拆分正則表達式匹配的那一刻
例子:A15
會分成A
1
和5
您需要遍歷您的令牌來連接這些令牌實際上是分隔符(使用不同的匹配器)
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
'[AZ]'包括字符' '[', '\\', ']', '^',「_ ','和''''''。在'new String(「foo」)'中包裝一個字符串沒有意義。 – 2015-02-09 18:44:31
字母數字令牌的範圍是什麼(從您的正則表達式看來,您正在從1到4 {1,4}每個Alpha和{1,4}每個數字。ABCD1234有效,但ABCDEF12345 NOT? – gtgaxiola 2015-02-09 19:26:28
正確,ABCD1234是有效的,而ABCDEF ...不是 – Naouk 2015-02-09 19:27:19