Java 将字符串匹配为二维整数数组的正则表达式

Java 将字符串匹配为二维整数数组的正则表达式,java,regex,multidimensional-array,Java,Regex,Multidimensional Array,我正在寻找一个正则表达式来识别字符串,这些字符串可以是具有相同长度列的二维整数数组 例如,这是一个字符串,我想将其转换为二维数组: 0 4 8 4\n9 6 7\n9 5 1 这可能是: 0 4 8 4 9 6 5 7 9 5 5 1 所以我提出了这个:[0-9]+[\t]?+\n |\r?{1,}但是它不检查列是否具有相同的长度。 感谢您的帮助。更新 如果您想直接使用正则表达式验证二维数组,可以构建模式来验证特定的x×y二维数组 public static void main(String[

我正在寻找一个正则表达式来识别字符串,这些字符串可以是具有相同长度列的二维整数数组

例如,这是一个字符串,我想将其转换为二维数组:

0 4 8 4\n9 6 7\n9 5 1

这可能是:

0 4 8 4
9 6 5 7
9 5 5 1
所以我提出了这个:[0-9]+[\t]?+\n |\r?{1,}但是它不检查列是否具有相同的长度。 感谢您的帮助。

更新 如果您想直接使用正则表达式验证二维数组,可以构建模式来验证特定的x×y二维数组

public static void main(String[] args) throws Exception {
    String data = "0 4 8 4\n9 6 5 7\n9 5 5 1";

    // Check if the data is either a 2 x 2 - 10 x 10 array
    for (int row = 2; row <= 10; row++) {
        for (int col = 2; col <= 10; col++) {
            Matcher matcher = Pattern.compile(buildPattern(row, col)).matcher(data);
            if (matcher.matches()) {
                System.out.printf("Valid %d x %d array%n", row, col);
                return;
            }
        }
    }
    System.out.println("Invalid 2d array");
}

public static String buildPattern(int row, int col) {
    StringBuilder patternBuilder = new StringBuilder();
    for (int r = 0; r < row; r++) {
        for (int c = 0; c < col; c++) {
            patternBuilder.append("\\d+");
            if (c + 1 < col) patternBuilder.append("[ ]");
        }
        if (r + 1 < row) patternBuilder.append("\n");
    }
    return patternBuilder.toString();
}
旧答案 我会做两次劈叉

拆分以获取行 拆分以从第一行获取列数 从那里,我将得到与第一行具有相同列数的行数。如果结果等于split 1中的行数,那么我们知道这是一个2d数组。否则,它就是一个锯齿状数组

public static void main(String[] args) throws Exception {
    String data = "0 4 8 4\n9 6 5 7\n9 5 5 1";

    // Get the rows
    String[] rows = data.split("[\r]?[\n]");

    // Get the number of columns in the first row
    int colCount = rows[0].split(" ").length;

    // Check if all rows have the same number of columns as the first row
    if (Arrays.stream(rows)
            .filter(row -> row.split(" ").length == colCount)
            .count() == rows.length) {
        System.out.println("Valid 2d array");
    } else {
        System.out.println("Jagged array");
    }
}
结果:

Valid 3 x 4 array
Valid 2d array
使现代化 如果您想直接使用正则表达式验证二维数组,可以构建模式来验证特定的x×y二维数组

public static void main(String[] args) throws Exception {
    String data = "0 4 8 4\n9 6 5 7\n9 5 5 1";

    // Check if the data is either a 2 x 2 - 10 x 10 array
    for (int row = 2; row <= 10; row++) {
        for (int col = 2; col <= 10; col++) {
            Matcher matcher = Pattern.compile(buildPattern(row, col)).matcher(data);
            if (matcher.matches()) {
                System.out.printf("Valid %d x %d array%n", row, col);
                return;
            }
        }
    }
    System.out.println("Invalid 2d array");
}

public static String buildPattern(int row, int col) {
    StringBuilder patternBuilder = new StringBuilder();
    for (int r = 0; r < row; r++) {
        for (int c = 0; c < col; c++) {
            patternBuilder.append("\\d+");
            if (c + 1 < col) patternBuilder.append("[ ]");
        }
        if (r + 1 < row) patternBuilder.append("\n");
    }
    return patternBuilder.toString();
}
旧答案 我会做两次劈叉

拆分以获取行 拆分以从第一行获取列数 从那里,我将得到与第一行具有相同列数的行数。如果结果等于split 1中的行数,那么我们知道这是一个2d数组。否则,它就是一个锯齿状数组

public static void main(String[] args) throws Exception {
    String data = "0 4 8 4\n9 6 5 7\n9 5 5 1";

    // Get the rows
    String[] rows = data.split("[\r]?[\n]");

    // Get the number of columns in the first row
    int colCount = rows[0].split(" ").length;

    // Check if all rows have the same number of columns as the first row
    if (Arrays.stream(rows)
            .filter(row -> row.split(" ").length == colCount)
            .count() == rows.length) {
        System.out.println("Valid 2d array");
    } else {
        System.out.println("Jagged array");
    }
}
结果:

Valid 3 x 4 array
Valid 2d array

如果需要,您可以使用这种图案添加可选CR:

(?m)^(?>(?>\\d+([ \\t]|$)(?=.*\\n(\\2?+\\d+\\1)))+\\n(?=\\2$))+.*
单击java按钮

对于第一行中的每个项目,先行检查下一行中是否存在同一列中的项目。要知道列是否相同,捕获组2包含可选的自引用\\2?+。这样,每次重复项目组并到达下一列时,捕获组2都会增长

详情:

?m使用多行模式 ^起跑线 ?>整条生产线的分组 ?>项目的组 \\d+[\\t]|$后跟空格/制表符或行尾的数字 ?=looakead .*\\n到达下一行 \\2?+\\d+\\1捕获组2 +重复项目组 \\n ?=\\2$检查下一行中是否没有更多列 +重复行组 *匹配下一行 注意:此模式检查分隔符是否唯一且不重复,并且始终与捕获组2中的[\\t]|$和\\1相同。不允许使用前导空格和尾随空格。但你可以用更灵活的方式来写:

(?m)^(?>[ \\t]*(?>\\d+[ \\t]*(?=.*\\r?\\n(\\1?+\\d+(?:[ \\t]+|[ \\t]*$))))+\\r?\\n(?=\\1$))+.*\\2$))+.*

这些模式既可以与匹配项一起使用以检查整个字符串,也可以在较大的字符串中查找最终的数组。

您可以使用这种模式来执行此操作。如果需要,请添加可选的CR:

(?m)^(?>(?>\\d+([ \\t]|$)(?=.*\\n(\\2?+\\d+\\1)))+\\n(?=\\2$))+.*
单击java按钮

对于第一行中的每个项目,先行检查下一行中是否存在同一列中的项目。要知道列是否相同,捕获组2包含可选的自引用\\2?+。这样,每次重复项目组并到达下一列时,捕获组2都会增长

详情:

?m使用多行模式 ^起跑线 ?>整条生产线的分组 ?>项目的组 \\d+[\\t]|$后跟空格/制表符或行尾的数字 ?=looakead .*\\n到达下一行 \\2?+\\d+\\1捕获组2 +重复项目组 \\n ?=\\2$检查下一行中是否没有更多列 +重复行组 *匹配下一行 注意:此模式检查分隔符是否唯一且不重复,并且始终与捕获组2中的[\\t]|$和\\1相同。不允许使用前导空格和尾随空格。但你可以用更灵活的方式来写:

(?m)^(?>[ \\t]*(?>\\d+[ \\t]*(?=.*\\r?\\n(\\1?+\\d+(?:[ \\t]+|[ \\t]*$))))+\\r?\\n(?=\\1$))+.*\\2$))+.*

这些模式可以与匹配项一起使用以检查整个字符串,也可以在较大的字符串中查找最终的数组。

您在问什么?列是如何分隔的?有空格,有标签,有吗?换句话说,项目是否可以包含空格?\n要为@casimirethippolyte列选择行和空格|选项卡,为什么需要使用正则表达式?它的用途远比人们想象的要有限,你可能只是在给自己制造另一个问题。@femchi也许代码更短,但你的it会变得不那么清晰,性能更高。你在问什么?列是如何分开的?有空格,有标签,有吗?换句话说,项目是否可以包含空格?\n要为@casimirethippolyte列选择行和空格|选项卡,为什么需要使用正则表达式?它的用途远比人们想象的要有限,你可能只是在给自己制造另一个问题。@femchi也许代码更短,但你的it会变得不那么清晰,性能更高。谢谢你的明确回答,但我很好奇,是否可以通过使用正则表达式来实现。我想说是这样,但模式是这样的
n可能必须为特定的x x x y数组设置。感谢您的明确回答,但我很好奇,是否可以通过使用正则表达式来设置。我想说是的,但模式可能必须为特定的x x x y数组设置。天哪,这太不可思议了+1您能从中分辨出尺寸,还是只是验证字符串是否类似于二维数组?@Shar1er80:谢谢。不,它只提取/验证数组,要知道维度,你必须使用更传统的方式,这些数字没有写在文本中。天哪,这太不可思议了+1您能从中分辨出尺寸,还是只是验证字符串是否类似于二维数组?@Shar1er80:谢谢。不,它只提取/验证数组,要知道维度,您必须使用更传统的方式,这些数字不会写入文本中。