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



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

例如

这是一个字符串我想把它转换成二维数组

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

可以是:

0 4 8 4
9 6 5 7
9 5 5 1

所以我想出了这个:"(([0-9]+[ t]?)+(n|r)?){1,}"但是它不检查列是否有相同的长度。谢谢你的帮助。

您可以使用这种模式(如果需要,添加可选的CR):

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

demo (点击Java按钮)

对于第一行中的每个项,前瞻检查下一行中是否存在同一列中的项。为了知道列是否相同,捕获组2包含一个可选的自引用\2?+。通过这种方式,每次重复"item"组(并到达下一列)时,捕获组2都会增长。

细节:

(?m) # use the multiline mode
^    # start of the line
(?>  # group for a complete line
    (?> # group for an item
        \d+ ([ \t]|$) # a number followed by a space/tab or the end of the line
        (?= # looakead
            .*\n # reach the next line
            (\2?+\d+\1) # capture group 2
        )
    )+ # repeat the item group
    \n 
    (?=\2$) # check if there isn't more columns in the next line
)+ # repeat the line group
.* # match the next line

注意:此模式检查分隔符是否唯一(不重复)并且始终与([ \t]|$)\1相同(在捕获组2中)。不允许前导和尾随空格。但是你可以用一种更灵活的方式来写:

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

这些模式既可以与matches()一起使用来检查整个字符串,也可以与find()一起使用来在更大的字符串中查找最终数组。

UPDATE

如果你想直接使用regex来验证2d数组,你可以构建模式来验证特定的"x by y"2d数组。

public static void main(String[] args) throws Exception {
    String data = "0 4 8 4n9 6 5 7n9 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();
}

结果:

Valid 3 x 4 array

老回答

我会做2次。

  1. 拆分获取行
  2. Split获取第一行的列数

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

public static void main(String[] args) throws Exception {
    String data = "0 4 8 4n9 6 5 7n9 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 2d array

最新更新