Java 拆分字符串以仅获取字母数字(但不是字母或数字)

Java split a string to get only alphanumeric (but not alpha or numeric)

目前我有以下代码:

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}时它只得到一个数字

我会使用分隔符,例如

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 拆分为 A 15

您需要遍历您的标记以连接那些实际上是定界符的标记(使用不同的匹配器)

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