多个组的正则表达式问题

时间:2011-11-03 19:56:01

标签: java regex

我正在尝试创建一个正则表达式模式以匹配以下格式的行:

field[bii] = float4:.4f_degree  // Galactic Latitude
field[class] = int2  (index) // Browse Object Classification
field[dec] = float8:.4f_degree (key) // Declination
field[name] = char20  (index) // Object Designation
field[dircos1] = float8   // 1st Directional Cosine

我想出了这种似乎有用的模式,然后突然似乎无法工作:

field\[(.*)\] = (float|int|char)([0-9]|[1-9][0-9]).*(:(\.([0-9])))

以下是我正在尝试使用的代码(编辑:提供完整方法而不是摘录):

private static Map<String, String> createColumnMap(String filename) {

    // create a linked hashmap mapping field names to their column types. Use LHM because I'm picky and
    // would prefer to preserve the order
    Map<String, String> columnMap = new LinkedHashMap<String, String>();

    // define the regex patterns
    Pattern columnNamePattern = Pattern.compile(columnNameRegexPattern);

    try {
        Scanner scanner = new Scanner(new FileInputStream(filename));
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();

            if (line.indexOf("field[") != -1) {
                // get the field name
                Matcher fieldNameMatcher = columnNamePattern.matcher(line);
                String fieldName = null;
                if (fieldNameMatcher.find()) {
                    fieldName = fieldNameMatcher.group(1);
                }

                String columnName = null;
                String columnType = null;
                String columnPrecision = null;
                String columnScale = null;
                //Pattern columnTypePattern = Pattern.compile(".*(float|int|char)([0-9]|[1-9][0-9])");
                Pattern columnTypePattern = Pattern.compile("field\\[(.*)\\] = (float|int|char).*([0-9]|[1-9][0-9]).*(:(\\.([0-9])))");
                Matcher columnTypeMatcher = columnTypePattern.matcher(line);

                System.out.println(columnTypeMatcher.lookingAt());

                if (columnTypeMatcher.lookingAt()) {
                    System.out.println(fieldName + ": " + columnTypeMatcher.groupCount());
                    int count = columnTypeMatcher.groupCount();
                    if (count > 1) {
                        columnName = columnTypeMatcher.group(1);
                        columnType = columnTypeMatcher.group(2);
                    }
                    if (count > 2) {
                        columnScale = columnTypeMatcher.group(3);
                    }
                    if (count >= 6) {
                        columnPrecision = columnTypeMatcher.group(6);
                    }
                }

                int precision = Integer.parseInt(columnPrecision);
                int scale = Integer.parseInt(columnScale);

                if (columnType.equals("int")) {
                    if (precision <= 4) {
                        columnMap.put(fieldName, "INTEGER");
                    } else {
                        columnMap.put(fieldName, "BIGINT");
                    }
                } else if (columnType.equals("float")) {
                    if (columnPrecision==null) {
                        columnMap.put(fieldName,"DECIMAL(8,4)");
                    } else {
                        columnMap.put(fieldName,"DECIMAL(" + columnPrecision + "," + columnScale + ")");
                    }
                } else {
                    columnMap.put(fieldName,"VARCHAR("+columnPrecision+")");
                }
            }

            if (line.indexOf("<DATA>") != -1) {
                scanner.close();
                break;
            }
        }

        scanner.close();
    } catch (FileNotFoundException e) {

    }

    return columnMap;
}

当我从Matcher对象获得groupCount时,它说有6组。但是,它们与文本不匹配,所以我绝对可以使用一些帮助......任何人都可以提供帮助吗?

2 个答案:

答案 0 :(得分:1)

我不完全清楚你的目标是什么,但我想出了以下模式,它接受了你所有输入的例子:

field\\[(.*)\\] = (float|int|char)([1-9][0-9]?)?(:\\.([0-9]))?

使用此代码:

    String columnName = null;
    String columnType = null;
    String columnPrecision = null;
    String columnScale = null;
    // Pattern columnTypePattern =
    // Pattern.compile(".*(float|int|char)([0-9]|[1-9][0-9])");
    // field\[(.*)\] = (float|int|char)([0-9]|[1-9][0-9]).*(:(\.([0-9])))
    Pattern columnTypePattern = Pattern
            .compile("field\\[(.*)\\] = (float|int|char)([1-9][0-9]?)?(:\\.([0-9]))?");
    Matcher columnTypeMatcher = columnTypePattern.matcher(line);

    boolean match = columnTypeMatcher.lookingAt();
    System.out.println("Match: " + match);

    if (match) {
        int count = columnTypeMatcher.groupCount();
        if (count > 1) {
            columnName = columnTypeMatcher.group(1);
            columnType = columnTypeMatcher.group(2);
        }
        if (count > 2) {
            columnScale = columnTypeMatcher.group(3);
        }
        if (count > 4) {
            columnPrecision = columnTypeMatcher.group(5);
        }
        System.out.println("Name=" + columnName + "; Type=" + columnType + "; Scale=" + columnScale + "; Precision=" + columnPrecision);
    }

我认为正则表达式的问题在于需要使比例和精度可选。

答案 1 :(得分:0)

field\[(.*)\] = (float|int|char)([0-9]|[1-9][0-9]).*(:(\.([0-9])))

.*过于宽泛,([0-9]|[1-9][0-9])中存在大量冗余,我认为以:.*开头的括号内组应该是可选的。

删除所有歧义后,我得到了

field\[([^\]]*)\] = (float|int|char)(0|[1-9][0-9]+)(?:[^:]*(:(\.([0-9]+))))?